0

在查看 InventoryCategory 摘要时,我可以获取 Inventory 但不能获取 InventoryImages。CakePHP 报告的错误是[ Model "Inventory" is not associated with model "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 回答