douyang2530 2014-05-18 08:53
浏览 61
已采纳

CakePHP:加入操作不起作用

I have Tables

Items, Attributes,Taxonomies, Taxonomy_attributes,Item_attributes.

Taxonomy_attributes have two fields attribute_id(which is a foreign key to id of attributes table) and taxonomy_id (which is a foreign key to id of taxonomies table). On the other hand Item_attributes have two fields attribute_id(which is a foreign key to id attributes table) and item_id (which is a foreign key to of id items table). Attributes table have following fields :- Name, Type and Checkable(which is either 0 or 1). Items table have fields id and model. Taxonomies table have fields if and name.

I want to add a method to Attribute model ,that returns list of all attributes with checkable equals 1 and join with items and taxonomies ,that return item model & taxonomy name for each attribute.

My code is as follows:-

public function getCheckables($checkable)
{
    $data =  $this->find('all',array(
                'fields' => array('Attribute.name', 'Attribute.type', 'Item.model', 'Taxonomy.name'),
                'conditions' => array('Attribute.checkable' => 1),
                'joins' =>  array(
                            array(
                                 'table' => 'item_attributes',
                                'alias' => 'ItemAttribute',
                                'type' => 'INNER',
                                'conditions' => 'ItemAttribute.Item_id = Item.id',
                             ),
                             array( 
                                'table' => 'items',
                                'alias' => 'Item',
                                'type' => 'INNER',
                                'conditions' => 'ItemAttribute.item_id = Item.id'
                            ),
                             array( 
                                'table' => 'taxonomy_attributes',
                                'alias' => 'TaxonomyAttribute',
                                'type' => 'INNER',
                                'conditions' => 'TaxonomyAttribute.Taxonomy_id = Taxonomy.id'
                            )

                    ),
                    'recursive'=>-1
                )
                );  
                pr($data); die();   
}

Can anybody guide me with the right code?

  • 写回答

2条回答 默认 最新

  • dplo59755 2014-05-18 09:50
    关注

    2 of your joins have the same condition : 'conditions' => 'ItemAttribute.Item_id = Item.id', in case of the first one, Item table was not yet joined and in case of the second one, because your first condition was wrong, the ItemAttribute table was not joined.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式