Thursday, December 1, 2011

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

Wow, CakePhp is legit. Thank you Tilen Majerie.

On Dec 1, 2:38 pm, Tilen Majerle <tilen.maje...@gmail.com> wrote:
> http://book.cakephp.org/2.0/en/models/associations-linking-models-tog...
> look
> for hasMany association :)
> --
> Lep pozdrav, Tilen Majerlehttp://majerle.eu
>
> 2011/12/1 Tilen Majerle <tilen.maje...@gmail.com>
>
>
>
>
>
>
>
> > do you have associations created ?
>
> > you should have something like that
>
> > Class hasMany Period
> > Class hasMany Post
> > Period hasMany Post (add here assocation key "dependent" => true) //so
> > this will delete all posts for given period_id when you delete some period
> > where Post.period_id = Period.id
>
> > Period belongsTo Class
> > Post belongsTo Class
> > Post belongsTo Period
>
> > am i right ? :)
>
> > I hope you understand :)
>
> > --
> > Lep pozdrav, Tilen Majerle
> >http://majerle.eu
>
> > 2011/12/1 GG <jairusk...@gmail.com>
>
> >> 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 sitehttp://ask.cakephp.organd 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
> >> athttp://groups.google.com/group/cake-php

--
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: