0

InventoryCategory の概要を見ていると、Inventory は取得できますが、InventoryImages は取得できません。CakePHP によって報告されるエラーは [ モデル "Inventory" はモデル "InventoryImage" に関連付けられていません] です。これらは私が採用しているモデルです:

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;
}

}
4

1 に答える 1

0

問題は、宣言$hasManyすることで、親のクラスから配列 InventoryCategoryを置き換えて消去することです。$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'
    );
}
于 2013-05-09T12:47:20.090 に答える