【发布时间】:2012-03-13 03:37:06
【问题描述】:
查看 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;
}
}
【问题讨论】:
-
'recursive' => 2在你的 find() 调用中吗?另外,请发布出现该错误的 find 调用。 -
没有。我不使用递归。改为包含。
-
codepublic function view() { if( !$this->params['id'] ) { $this->redirect( $this->referer() ); } $conditions = ( is_numeric( $this->params['id'] ) ) ?数组( $this->modelClass . '.id = ' => $this->params['id'] ) : 数组( $this->modelClass . '.slug = ' => $this->params['id '] ); $item = $this->{$this->modelClass}->find( 'first' , array( 'conditions' => $conditions , 'contain' => $this->{$this->modelClass}->包含模型())); $this->set(compact('item')); }code
标签: cakephp model model-associations