1

There are posts and comments table.

For one post there are multiple comments.

If i delete one post will the comments for that post be automatically deleted in cakephp ?

4
  • book.cakephp.org/view/1039/Associations-Linking-Models-Together (look for "dependent") Commented Sep 1, 2011 at 7:40
  • do you want them to be deleted? Commented Sep 1, 2011 at 7:41
  • Yes, i want to delete those comments with that post. How can i do that ? Commented Sep 1, 2011 at 7:56
  • Have you tried it? book.cakephp.org/view/1036/delete: "Deletes the record identified by $id. By default, also deletes records dependent on the record specified to be deleted." Commented Sep 1, 2011 at 8:30

3 Answers 3

6
var $hasMany = array(
  'Comment' => array(
     'className' => 'Comment',
     'foreignKey' => 'post_id',
     'dependent'=> true
   )
); 
Sign up to request clarification or add additional context in comments.

1 Comment

'dependent'=> true is the key !
1

This really shouldn't be up to CakePHP... let your database handle that by using FOREIGN KEY constraints and set it to ON DELETE CASCADE

Comments

0

If you are using the foreign key Constraint ON DELETE CASCADE, yes.

See here for documentation (works with engine InnoDB)

1 Comment

I didn't use foreign key constraint. var $hasMany=array('Comment'); and var $belongsTo = array('Post');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.