【问题标题】:CakePHP: Join Operation not workingCakePHP:加入操作不起作用
【发布时间】:2014-05-18 08:53:13
【问题描述】:

我有桌子

Items, Attributes,Taxonomies, Taxonomy_attributes,Item_attributes.

Taxonomy_attributes 有两个字段 attribute_id(它是属性表的 id 的外键)和 taxonomy_id(它是分类表的 id 的外键)。 另一方面,Item_attributes 有两个字段 attribute_id(它是 id 属性表的外键)和 item_id(它是 id 项目表的外键)。 属性表具有以下字段:- 名称、类型和可检查(0 或 1)。 项目表具有字段 id 和 model。 分类表具有 if 和 name 字段。

我想向属性模型添加一个方法,它返回所有可检查属性的列表等于 1 并加入项目和分类,返回每个属性的项目模型和分类名称。

我的代码如下:-

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();   
}

谁能用正确的代码指导我?

【问题讨论】:

  • 您的模型中是否与其他 3 个定义了任何类型的关系?

标签: php mysql sql cakephp join


【解决方案1】:

您的 2 个联接具有相同的条件:'conditions' => 'ItemAttribute.Item_id = Item.id',如果是第一个,Item 表尚未加入,如果是第二个,因为您的第一个条件错误,ItemAttribute 表没有加入。

【讨论】:

  • 你是对的。两个连接都具有相同的条件。但也有一些其他问题。我接受你的回答,因为你的回答是正确的。 @Tanatos
【解决方案2】:

实际解决方案

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.attribute_id = Attribute.id',
                                 ),
                                 array( 
                                    'table' => 'items',
                                    'alias' => 'Item',
                                    'type' => 'INNER',
                                    'conditions' => 'ItemAttribute.item_id = Item.id'
                                ),
                                  array( 
                                    'table' => 'taxonomies',
                                    'alias' => 'Taxonomy',
                                    'type' => 'LEFT',
                                    'conditions' => 'Item.category_id = Item.id'
                                )



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

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多