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 が 2 つのクエリを実行してその結果を返すため、最小オンを要求する方法が見つかりません。

要件は次のとおりです。システム全体PhotoSmallで少なくとも 1 つの製品のみを表示できます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

さらに、contains を使用する必要があります - 少なくとも私にとってはうまくいきました (recursive=-1 のため):

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

結合はセットアップ構成のみです。データを指定しないと、1 つのモデルのデータのみが取得されます。

包含可能な動作を使用しますか?最も簡単な解決策になります。そうでない場合は、再帰を 0 または 1 に設定してみてください。

PS: '条件' 内の 2 番目のエイリアスが間違っています (PhotoSmall である必要があります)。

于 2012-01-11T20:46:46.870 に答える
0

まず、recursive を -1 に設定しています。これにより、どのような検索を行っても Product データが得られるはずです。

次のように、 Product モデルの検索で AND 条件を実行してみませんか。

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

注: 関連するデータが必要なため、recursive は少なくとも 1 つ必要です。

于 2012-01-11T20:52:51.063 に答える