0
<?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,
);
4

2 回答 2

0

首先,您将递归设置为 -1,无论您进行何种查找,它都应该为您提供产品数据。

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

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

注意:recursive 至少需要为一个,因为您需要相关数据。

于 2012-01-11T20:52:51.063 回答
0

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

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

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

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

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

于 2012-01-11T20:46:46.870 回答