du13932014807 2011-02-10 20:26
浏览 26

选择嵌套HABTM数组的特定字段

I have a tickets table, and a contacts table. A ticket can have many contacts.

I am paginating the tickets table, and want it to select specific fields including the first linked contact form the nested array. No matter what I try, I can't seem to figure this one out (or if it is even possible).

My code:

$this->paginate = array(<br>
    'conditions' => array('status_id !=' => '3'),<br>
    'limit'=>50,<br>
    'fields'=>array('Ticket.title', 'Ticket.ticket_number', 'Priority.name', 'Status.name', 'Contact.0.full_name')  <br>            
);

(The Contact.0.full_name is causing it to fail. How can I make this work?)

So I can use this column with $this->Paginator->sort.

  • 写回答

1条回答 默认 最新

  • doutongwei4380 2011-02-10 21:24
    关注

    You cant call columns like this Contact.0.full_name CakePHP doesn't work like that. A valid field name is TableAlias.column_name

    You cant use hasMany relation's children into sort operation.

    To achieve similar functionality, You can drop hasMany and add hasOne to achieve the desired output. Because there's no schema changes in both relationship types.

    $this->Ticket->unbindModel(array('hasMany' => array('Contact')));
    $this->Ticket->bindModel(array('hasOne' => array('Contact')));
    $this->paginate = array(
        'conditions' => array('status_id !=' => '3'),
        'limit'=>50,
        'fields'=>array('Ticket.title', 'Ticket.ticket_number', 'Priority.name',  'Status.name', 'Contact.full_name')
    );
    

    Now you can use this column Contact.full_name for sorting purposes.

    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启