如何在CakePhp中仅获取带有图像的产品

时间:2012-01-11 17:15:09

标签: cakephp find model-associations

<?php
class Product extends AppModel {
    var $name = 'Product';

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Brand'
    );


    var $hasOne = array(
        'PhotoSmall' => array(
            'className' => 'Photo',
            'foreignKey' => 'product_id',
            'dependent' => true,
            'conditions' => 'PhotoSmall.tipo = "small"',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
    );

    var $hasMany = array(
        'PhotoBig' => array(
            'className' => 'Photo',
            'foreignKey' => 'product_id',
            'dependent' => true,
            'conditions' => 'PhotoBig.tipo = "big"',
            'fields' => '',
            'order' => 'PhotoBig.order',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Rating' => array(
            'className' => 'Rating',
            'foreignKey' => 'product_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

我需要我的所有发现(管理区域除外)才能仅检索带有图像的产品。

如果PhotoSmall很简单,我可以在条件中执行PhotoSmall.id IS NOT NULL,因为CakePhp生成左连接,但我找不到一种方法要求PhotoBig因为CakePhp会执行两个查询来返回结果。

要求如下,我只能显示系统范围为PhotoSmall和至少一个PhotoBig的产品(网站的管理部分除外),该解决方案应与CakePhp分页一起使用

我尝试了以下代码但没有成功,它只返回产品数据,而不是照片数据:

$this->Product->recursive = -1;
$conditions = array('Product.category_id'=> $cats);
$joins = array(
    array('table' => 'photos',
        'alias' => 'PhotoBig',
        'type' => 'INNER',
        'conditions' => array(
            'Product.id = PhotoBig.product_id',
        )
    ),
    array('table' => 'photos',
        'alias' => 'PhotoSmall',
        'type' => 'INNER',
        'conditions' => array(
            'Product.id = PhotoBig.product_id',
        )
    )
);
$this->paginate = array(
    'limit' => 12,
    'conditions' => $conditions,
    'joins' => $joins,
);

2 个答案:

答案 0 :(得分:0)

你需要额外使用包含 - 至少对我有用 (由于你的递归= -1):

'contain' => array('PhotoSmall', 'PhotoBig'),

联接只是设置配置。如果不指定数据,您将只获得一个模型的数据。

你使用可包含的行为吗?将是最简单的解决方案。 如果没有,请尝试将递归设置为0或1。

PS:你的第二个别名在'条件'中是错误的(应该是PhotoSmall)。

答案 1 :(得分:0)

首先,你将递归设置为-1,无论你做什么样的搜索都应该只给你产品数据。

为什么不在产品模型的查找中执行AND条件,如下所示:

    $this->Product->find('all', array('recursive' => 1, 'conditions' => array(
    "NOT" => array('PhotoSmall.id' => NULL, 'PhotoBig.id' => NULL))));

注意:递归需要至少为1,因为您需要相关数据。