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条)

报告相同问题?

悬赏问题

  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面