Orphan rows are being left in the table
I have the following models:
class Page extends AppModel {
var $name = 'Page';
var $order = array('Page.modified' => 'desc');
var $hasOne = array(
'Post' => array(
'className' => 'Post',
'dependent' => TRUE
));
class Post extends AppModel {
var $name = 'Post';
var $hasAndBelongsToMany = array(
'Category' => array(
'className' => 'Category'
)
);
var $belongsTo = array(
'Page' => array(
'className' => 'Page',
'dependent' => TRUE
)
);
Here is the controller code:
function admin_delete($id = NULL) {
if ($this->Post->delete($id, TRUE)) {
$this->Session->setFlash('This Post has been deleted', 'flash_good');
$this->redirect(array('action' => 'index'));
}
}
My tables are as follows:
Page:
---------------------------
id title 开发者_运维百科uri meta_keywords
Post:
---------------------------
id page_id title uri body
Whenever, I try to delete a Post, the Page related to the Post, doesn't get deleted and is left in the table. What should I do for the related Page row to be deleted when I delete the Post?
The sort answer is: Delete the page and this will delete the post too. :)
The long explanation: Dependent works only for hasMany or hasOne, in other words it will delete childs of the parent record as well. In your schema your child id the post, because it contain foreign key of the pages table.
精彩评论