2012-01-11 15 views
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の場合 CakePhpで画像付きの製品を取得する方法

が簡単で、CakePHPは左結合を生成しますので、私は条件に PhotoSmall.id IS NOT NULLを行うことができます、しかし、私は、CakePHPは、その結果を返すために、2つのクエリを行いますので、 PhotoBigに最低を必要とする方法を見つけることができません。

要件は以下の通りです、私は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, 
); 

答えて

0

- 私 のために働いている、少なくとも(によるあなたの再帰的に= -1):

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

ジョインはセットアップ構成のみです。データを指定せずに、1つのモデルのデータのみを取得します。

あなたは封じ込める行動を使用していますか?最も簡単な解決策になります。 そうでない場合は、0または1に再帰的に設定してください。

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

0

まず、再帰的な値を-1に設定すると、どのような検索を行っても商品データが得られるはずです。

は、なぜあなたはそうのような製品のモデルに検索でAND条件をしない:

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

注:再帰を使用すると、関連するデータをしたいので、少なくとも一つをする必要があります。

関連する問題