2012-03-13 11 views
0

InventoryCategoryのサマリーを見ると、インベントリは取得できますが、InventoryImagesは取得できません。 CakePHPによって報告されたエラーは[Model "Inventory"はモデル "InventoryImage"に関連付けられていません。これらは私が使用していたモデルです。CakePHPモデル拡張アソシエーション

class InventoryCategory extends InventoriesAppModel { 
public $hasMany = 
    array(
      'Inventory' 
     , 'InventoryCategoryImage' => array 
      (
        'className' => 'Media.MediaImage' 
       , 'foreignKey' => 'foreign_key' 
       , 'conditions' => array(
           'InventoryCategoryImage.model' => 'InventoryCategoryImage' 
          , 'InventoryCategoryImage.group' => 'Inventory Category Image' 
          , 
       ) 
       , 'dependent' => true 
       , 
      ) 
     , 
    ); 

public function containedModels() 
{ 
    $contain = array(
       'Inventory' 
      , 'InventoryCategoryImage' 
      , 
    ); 
    return $contain; 
} 

} 

class Inventory extends InventoriesAppModel { 

public $belongsTo = 
    array(
      'User' 
     , 'InventoryCategory' 
     , 
    ); 

public $hasMany = 
    array(
      'InventoryImage' => array 
      (
        'className' => 'Media.MediaImage' 
       , 'foreignKey' => 'foreign_key' 
       , 'conditions' => array(
           'InventoryImage.model' => 'InventoryImage' 
          , 'InventoryImage.group' => 'Inventory Image' 
          , 
       ) 
       , 'dependent' => true 
       , 'order' => 'InventoryImage.rank ASC, InventoryImage.id ASC' 
      ) 
     , 
    ); 

public function containedModels() 
{ 
    $contain = array(
       'User' 
      , 'InventoryCategory' 
      , 
    ); 
    return $contain; 
} 

} 
+0

があなたの検索では=> 2 ' ' '再帰的な' です()を呼び出しますか?また、そのエラーを示す検索呼び出しを投稿してください。 – Costa

+0

いいえ、再帰的に使用しません。代わりにContainを含む。 – radarhill

+0

'code'public関数ビュー(){ 場合 {\tの$ this - >リダイレクト(の$ this - >リファラ()()の$ this - > paramsは[ 'ID']!)。 } $条件\t =(is_numeric($ this-> params ['id'])) \t \t \t?配列($ this-> modelClass。 '.id =' => $ this-> params ['id']) \t \t \t:array($ this-> modelClass。 '.slug =' => $ this-> params ['id']); $項目\t \t = $この - > {の$ this - > modelClass} - >検索( '第' \t \t \t \t \t \t、アレイ( \t \t \t \t '条件' \t => $条件 を\t \t \t \t、\t => $これを '含む' - > {の$ this - > modelClass} - > containedModels() \t \t \t) \t); $ this-> set(compact( 'item')); } 'code' – radarhill

答えて

0

問題がInventoryCategory$hasManyを宣言することによって、あなたが代わるとその親のクラスから$hasMany配列を消去するということです。

したがって、$hasMany宣言には、親の関連付けが含まれていなければなりません。

異なるアプローチは、親の$hasMany配列に新しい関連付けを追加することです。
ここでの質問は、使用するコールバックです。私は知らない(私も答えを探している)。これまでのところ私はpublic function __construct()が最も適していると思います。このよう

public function __construct($id = false, $table = null, $ds = null) { 
    parent::__construct(); 

    // adding Blog-specific Card-association. 
    // Blog is a class extending some other class that does not know about the Class association. 
    $this->belongsTo['Card'] = array(
     'className' => 'Card', 
     'foreignKey' => 'card_id' 
    ); 
} 
関連する問題