Re: intercepting HABTM inserts - Where?
I was stumbling because I thought the beforeSave() was an all or nothing thing. So if you return true, the save functionthe ENTIRE save function executes. So I was trying to query bits of the save function to specifically stop the insert query on the HABTM table. Now I realize that if I unset the key variable of the HABTM relationship, cake knows to not make the insert. So here's the solution in the User model to the above problem: function beforeSave() { if ($this->data['User']['role_id'] != 'Club Administrator') { unset($this->data['Club']['Club']); } } FOLLOW-UP QUESTION: Is there a way to detect the action the controller is executing so I can wrap the if..then in a condition like: if ($this->action) { } ...that of course does not work...but what would get me the current users_controller action? Thanks! On Mar 4, 6:19 pm, brian wrote: > I would do that in beforeSave(). What problems were you having with that? > > On Wed, Mar 4, 2009 at 1:34 PM, foldiman wrote: > > > I've successfully setup a HABTM between a User model and Club model > > that looks like this in my User model: > > var $hasAndBelongsToMany = array( > > 'Club' =>array( > > 'className' => 'Club', > > 'foreignKey' => 'user_id', > > 'associationForeignKey' => 'club_id' > > ) > > ); > > > When creating new Users, I display a select field using the following > > in my Controller. > > > $this->set('clubs', $this->User->Club->find('list')); > > > However, after saving a new user, I only want to insert records into > > my clubs_users table for certain types of Users. For example, if a > > User is a 'Club Administrator', they can have a relationship with a > > Club. If they are an 'Anonymous User', I don't want to associate them > > with a Club. > > > I've successfully got this running by using jQuery to hide and show > > the Clubs select field on the add new User page according to a > > preceding User Role field. jQuery actually sets the Clubs field's name > > to 'null' when hidden so Cake ignores the field. However, this sees > > like a fragile system. > > > Is there a built in way in Cake to achieve the same results? Maybe in > > the beforeSave() callback in the User model to intercept and kill the > > clubs_users table insert? I've tried a few things there without luck. > > Any help would be appreciated. Thanks. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: intercepting HABTM inserts - Where?
I would do that in beforeSave(). What problems were you having with that? On Wed, Mar 4, 2009 at 1:34 PM, foldiman wrote: > > I've successfully setup a HABTM between a User model and Club model > that looks like this in my User model: > var $hasAndBelongsToMany = array( > 'Club' =>array( > 'className' => 'Club', > 'foreignKey' => 'user_id', > 'associationForeignKey' => 'club_id' > ) > ); > > When creating new Users, I display a select field using the following > in my Controller. > > $this->set('clubs', $this->User->Club->find('list')); > > However, after saving a new user, I only want to insert records into > my clubs_users table for certain types of Users. For example, if a > User is a 'Club Administrator', they can have a relationship with a > Club. If they are an 'Anonymous User', I don't want to associate them > with a Club. > > I've successfully got this running by using jQuery to hide and show > the Clubs select field on the add new User page according to a > preceding User Role field. jQuery actually sets the Clubs field's name > to 'null' when hidden so Cake ignores the field. However, this sees > like a fragile system. > > Is there a built in way in Cake to achieve the same results? Maybe in > the beforeSave() callback in the User model to intercept and kill the > clubs_users table insert? I've tried a few things there without luck. > Any help would be appreciated. Thanks. > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
intercepting HABTM inserts - Where?
I've successfully setup a HABTM between a User model and Club model that looks like this in my User model: var $hasAndBelongsToMany = array( 'Club' =>array( 'className' => 'Club', 'foreignKey' => 'user_id', 'associationForeignKey' => 'club_id' ) ); When creating new Users, I display a select field using the following in my Controller. $this->set('clubs', $this->User->Club->find('list')); However, after saving a new user, I only want to insert records into my clubs_users table for certain types of Users. For example, if a User is a 'Club Administrator', they can have a relationship with a Club. If they are an 'Anonymous User', I don't want to associate them with a Club. I've successfully got this running by using jQuery to hide and show the Clubs select field on the add new User page according to a preceding User Role field. jQuery actually sets the Clubs field's name to 'null' when hidden so Cake ignores the field. However, this sees like a fragile system. Is there a built in way in Cake to achieve the same results? Maybe in the beforeSave() callback in the User model to intercept and kill the clubs_users table insert? I've tried a few things there without luck. Any help would be appreciated. Thanks. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---