php - CakePHP:加入操作不起作用

标签 php mysql sql cakephp join

我有 table

Items, Attributes,Taxonomies, Taxonomy_attributes,Item_attributes.

Taxonomy_attributes 有两个字段 attribute_id(属性表 id 的外键)和taxonomy_id(分类表 id 的外键)。 另一方面,Item_attributes 有两个字段 attribute_id(它是 id 属性表的外键)和 item_id(它是 id items 表的外键)。 属性表具有以下字段:名称、类型和可检查(0 或 1)。 Items 表有字段 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();   
}

有人可以指导我使用正确的代码吗?

最佳答案

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

关于php - CakePHP:加入操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720186/

相关文章:

mysql - rails 5.1 : Primary Key is not a BIGINT

sql - Oracle 虚拟列在一个实例上生成 -1.0002...而不是 -1

php - 从库中拍照并将图像上传到服务器 swift 4

php - 第一次连接阻止了对 PHP 的多个请求

php - mysqli_fetch_array 在 php 7 中不起作用

php - 使用 ajax Php 和 mysql 级联下拉菜单

mysql - 差距检测 - 识别跳过/错过的日期

sql - 如何用 "group by"子句替换 "having"的嵌套选择?

php - 使用 PDO 不会插入任何内容

php - 使用数据库生成的列表时传递 PHP 变量而不被看到