Thursday, December 1, 2011

How to remove entries from two tables, from a controller delete() function

Right now, my PeriodsController.php delete() function, deletes entries
in my periods table. (Not a problem).
My PostsController.php delete() function, deletes entries in my posts
table. (Also not a problem.)
(The problem) = I would like my PeriodsController.php delete(), to
also delete any posts (in my posts table), where they have a matching
period_id.


///Example: I am a teacher who has a science class. I post homework
under my science period. When I want go to delete the science class
period, I would like it to also delete all of the posts I made under
that class period...


So......

I have a periods table:

period_id | title | user_id

I also have a posts table:

id | title | body | created | modified | user_id | period_id

Here is my PeriodsController.php

function delete($id) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}

if($this->isActionable($id)){
if ($this->Period->delete($id)) {
$this->Session->setFlash('Class successfully deleted.');
$this->redirect(array('controller' => 'posts', 'action' =>
'index'));
}
} else {
$this->Session->setFlash('You cannot delete that class.');
$this->redirect(array('controller' => 'posts', 'action' =>
'index'));
}
}


Here is my PostsController.php

function delete($id) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}

if($this->isActionable($id)){
if ($this->Post->delete($id)) {
$this->Session->setFlash('Post successfully deleted.');
$this->redirect(array('action' => 'index'));
}
} else {
$this->Session->setFlash('You cannot delete that post.');
$this->redirect(array('action' => 'index'));
}
}

Any help is greatly appreciated. Thanks!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: