我有两个模特。一个是用户,另一个是UserProfile,并且分别创建了表users和user_profiles。我想当我删除用户数据时,相关的UserProfile也应该自动删除。我创建了hasone并且属于关系。我还使用带有删除按钮的复选框删除数据,这样我就可以一次删除多条记录。
public $hasOne = array(
'UserProfile' => array(
'className' => 'UserProfile',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
在UsersController中我有一个deleteSelected函数
public function deleteSelected()
{
foreach($this->data['User'] as $key => $value)
{
if($value != 0)
{
$this->User->delete($value);
}
}
$this->redirect($this->referer());
}
finaaly我收到错误:在数据源默认情况下找不到模型GroupsUser的表groups_users。请告诉我代码中的错误。感谢。
答案 0 :(得分:1)
将依赖设置为true
public $hasOne = array(
'UserProfile' => array(
'className' => 'UserProfile',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);