1 to 3 of 3
The models:
class Hood extends ActiveRecord
{
var $has_many = 'hoodmemberships';
}
class Hoodmembership extends ActiveRecord
{
var $belongs_to = 'hood';
}
The edit-a-hoodmembership-within-a-hood action in the Hood controller:
function edit_user()
{
// find Hood & Membership
$this->hood =& $this->Hood->find($this->params['id'],array('include'=>'hoodmemberships','conditions'=>"_hoodmemberships.user_id=".$this->params['userid']));
if(!empty($this->params['user']) && !empty($this->params['userid'])){
$this->hood->setAttributes($this->params['hood']);
$this->hood->save();
}
}
And the result when saving :
(mysqlt): SET AUTOCOMMIT=0
(mysqlt): BEGIN
(mysqlt): UPDATE hoods SET id=1, year=2007, master_1_user_id=0, etc ... WHERE id=1
(mysqlt): SELECT * FROM hoodmemberships WHERE hoodmemberships.user_id = '2' AND hoodmemberships.year = '2007' AND hoodmemberships.id <> '9' LIMIT 1
(mysqlt): UPDATE hoodmemberships SET id=9, hood_id=1, user_id=2, year=2007, is_enabled=1, etc ... WHERE id=9
(mysqlt): COMMIT
(mysqlt): SET AUTOCOMMIT=1
Smart Commit occurred
So an update for the fields in the associated hoodmemberships row is being triggered, just by calling $this->hood->save();
But I cannot get the new values in the update, probably because the fields in the view are not named properly.
I tried hood[hoodmemberships][0][fieldname] in the view (and other combinations) but the entered value is not picked up.
<?=$form_tag_helper->text_field_tag('hood[hoodmemberships][0][year]', $hood->hoodmemberships[0]->year);?>
Help plz !
Maybe I don't get your question.
But: look what you actually have in the $this->params-array.
If you have a form that collects information for two (related) models, then:
$this->hood->hoodmemberships[0]->setAttributes($this->params['hood']['hoodmemeberships'][0])
?
Of course!
I tried setAttributes before, but without referencing to one instance ( [0] ). ..it's only normal that it produced an error because it's a has_many relationship.
Thanks again, Kaste!
And since it's only one membership being edited I can simplify the view. Now it's:
<?=$form_tag_helper->text_field_tag('hoodmembership[fieldname]', $hood->hoodmemberships[0]->fieldname);?>
in the view, and:
$this->hood->hoodmemberships[0]->setAttributes($this->params['hoodmembership']);
in the controller
1 to 3 of 3