在查看InventoryCategory摘要时,我可以获得库存,但不能获得库存图像。 CakePHP报告的错误是[模型“库存”与模型“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;
}
}
答案 0 :(得分: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'
);
}