2016-04-29 7 views
0

私のcakephpのアプリケーションは、私が唯一の製品モデルの内容は、(ProductDimensionを製品の関連付けを取得する方法があります来ているすべてのカテゴリーの要素を見つけています関係船cakephpの検索再帰協会

class Category extends AppModel { 
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product', 
'joinTable' => 'products_categories', 
'foreignKey' => 'category_id', 
     'associationForeignKey' => 'product_id', 
     'unique' => 'keepExisting', 
    ), 
); 
} 

class Product extends AppModel { 
public $primaryKey = 'id'; 
public $hasMany = array(
    'ProductSwatch' => array(
     'className' => 'ProductSwatch', 
     'foreignKey' => 'product_id', 
     'dependent' => true 
    ), 
    'ProductDimension' => array(
     'className' => 'ProductDimension', 
     'foreignKey' => 'product_id', 
     'dependent' => true 
    ), 
    'ProductCare' => array(
     'className' => 'ProductCare', 
     'foreignKey' => 'product_id', 
     'dependent' => true 
    ), 
    'ProductDimension' => array(
     'className' => 'ProductDimension', 
     'foreignKey' => 'product_id', 
     'dependent' => false, 
    ), 
    'ProductCare' => array(
     'className' => 'ProductCare', 
     'foreignKey' => 'product_id', 
     'dependent' => false, 
    ), 
    'Review' => array(
     'className' => 'Review', 
     'foreignKey' => 'product_id', 
     'dependent' => true, 
    ) 
); 
} 

と、次のモデルを持っていますProductSwatches、etc.)を参照してください。

+0

が使用ContainableBehavior http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html – Salines

+1

常にタグを含めてください見ます使用しているCakePHPのバージョン。 – Dave

答えて

0

デフォルトでは、CakePHP 2再帰レベルは第1レベルの関連付けのみです。 recursiveオプションをfind(またはモデルプロパティ)として使用すると、ディープアソシエーションをフェッチできます。

モデルrecursiveプロパティの詳細については、以下を参照してください。たとえばhttp://book.cakephp.org/2.0/en/models/model-attributes.html#recursive

:はContainable振る舞いを使用している

$this->Category->find('all', array(
    'recursive' => 2, 
    'conditions' => array(), // Your conditions, etc. 
)); 

(とより良い方法)。

追加

公共$ actsAs =配列( '収容可能');

Containableの動作を有効にするモデルに設定してください。次に、あなたは:

$this->Category->find('all', array(
    'contain' => array(
     'Product' => array(
      'ProductSwatch', 
      'ProductDimension', 
      'ProductCare', 
      'Review', 
     ), 
    ), 
    'conditions' => array(), // Your conditions, etc. 
)); 

より深い関連付けをフェッチすることができます。詳細については

についてContainable行動と深く関連は、http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations