【问题标题】:CakePHP: How to filter results in the parent model using conditions in the hasMany associationsCakePHP:如何使用 hasMany 关联中的条件过滤父模型中的结果
【发布时间】:2013-12-31 06:44:01
【问题描述】:

CakePHP:2.4.3

我尝试按照以下问题的答案中的说明进行操作,但它仅在子模型中应用该条件。 (Find conditions with hasMany model)

例如,我希望能够仅获取 ProductsCategory.category_id 为 2 的产品。现在它只过滤关联的模型并仍然返回我的所有产品。

编辑: 第一次没有提到,但我希望能够有多个条件。 ProductsCategory.category_id == 2 AND ProductsTag.tag_id == 1。

我有这些模型: 产品、类别和标签

产品有很多 ProductsCategory 和 ProductsTag

ProductsCategory 和 ProductsTag 如下:

产品类别

product_id (fk) | category_id (fk)

产品标签

product_id (fk) | tag_id (fk)

Model/Product.php(已编辑以包含 Rikesh 的建议)

class Product extends AppModel {
public $primaryKey = 'product_id';

public $actsAs = array('Containable');

public $belongsTo = 'Supplier';
public $hasMany = array(
    'ProductsCategory' => array(
        'className' => 'ProductsCategory',
        'foreignKey' => 'category_id',
        'dependent' => true),
    'ProductsTag' => array(
        'className' => 'ProductsTag',
        'foreignKey' => 'tag_id',
        'dependant' => true
    ));

到目前为止,这是我尝试过的:

$this->Product->find('all',
            array(
                'recursive' => -1,
                'contain' => array(
                    'ProductsCategory' => array(
                        'conditions' => array(
                            'ProductsCategory.category_id' => 2)
                        )
                    )
                )
            )

$this->Product->find('all',
            array(
                'joins' => array(
                    array(
                        'table' => 'products_categories',
                        'alias' => 'ProductsCategory',
                        'type' => 'inner',
                        'conditions' => array('ProductsCategory.category_id' => 2)
                    )
                )
            )
        )

【问题讨论】:

    标签: php cakephp model cakephp-2.4


    【解决方案1】:

    因为 ProductsCategory belongsTo Product 你可以找到 productsCategories 并让 cake 加入到产品中

    $this->Product->ProductsCategory->find(
        'all',
        array(
            'conditions' => array(
                'ProductsCategory.category_id' => 2,
                'ProductsTag.tag_id' => 1
        ),
            'contain' => array('Product', 'Product.ProductTag'),
            'group' => 'ProductsCategory.product_id'
        )
    )
    

    【讨论】:

    • 谢谢,它成功了 :) 我意识到我没有要求这个,但是否可以在多个关联中搜索条件?例如 ProductsCategory.category_id == 2 AND ProductsTag.tag_id == 1?
    • 我得到这个:SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列'ProductsTag.tag_id'。查看查询我可以看到它确实不在 SELECT 子句中。注意:我修复了 Product 中“包含”行中缺少的 s。
    【解决方案2】:

    我设法通过在我的加入条件中添加 ProductsCategory.product_id = Product.product_id 来做我想做的事:

    $this->Product->find('all', array(
                    'joins' => array(
                        array(
                            'table' => 'products_categories',
                            'alias' => 'ProductsCategory',
                            'type' => 'inner',
                            'conditions' => array(
                                'ProductsCategory.product_id = Product.product_id',
                                'ProductsCategory.category_id' => array(2, 3), 
                            )
                        ),
                        array(
                            'table' => 'products_tags',
                            'alias' => 'ProductsTag',
                            'type' => 'inner',
                            'conditions' => array(
                                'ProductsTag.product_id = Product.product_id',
                                'ProductsTag.tag_id' => 1
                            )
                        )
                    )
                )
            )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 2010-12-17
      • 2023-03-16
      相关资源
      最近更新 更多