I'm beginner with the MVC concept and with Yii, but I have already he documentation and i didn't find an especific topic for my issue, and as i have some urgency, i decided to post here.
I have two diferent tables in my db and I'd like to edit it's data in the same page (view). Te table "users" has information about my system's users, and the table "usergroups" has informations about the groups that the users are member of (this table has foreign keys of the "users" and "groups" tables n-n relationship).
I need that the admin user of my system, while he create or edit an user's data, he could define also (by checking or not checkboxes) what are the groups of my users. I dont know how to acces and edit two different tables from the same view (using controllers and models)
Yes, i've checked this cookbook and i've made some tries with it's approach. Bu my problem is a bit more specific. Actually, I've been experiencing problems on how to show and update in the view 'users/create' the data collected from the models 'users' and 'usergroups' by using checkboxes to "check" and "uncheck" the groups in which the user is assigned or not. For example, in my "usergroups" table i have the below (wich means: user 1 belongs to group 3, and user 2 belongs to 1 and 4):
[table]
[tr]
[td]idUser[/td]
[td]idGroup[/td]
[/tr]
[tr]
[td]1[/td]
[td]3[/td]
[/tr]
[tr]
[td]2[/td]
[td]1[/td]
[/tr]
[tr]
[td]2[/td]
[td]4[/td]
[/tr]
[/table]
So, I'd like that when my "users/Update" view is rendered to update the user 2, only the checkboxes 1 and 4 would be checked, and all others unchecked. And if I change these checkboxes, the correspondig changes would be done in the table "usergroups" when the update action is called. I need just some hints to begin it. Like "how" or "where" in the framework i must code to begin it, and what methods or widgets are more usefull to me in this case…
I just implemented what you want (Users table -> UserGroup table <- Groups table).Unfortunatelly I don't have the code in my home PC so I can get the code on Monday.
But the only thing you have to do is overide the afterSave function in your users model
Something like
protected function afterSave() {
//delete the user's relations in usergroup table
UserGroup::model()->deleteAll("user_ID =".$this->user_ID);
//get the new groups from the $_POST and save them to usergroup table
foreach ($_POST["groups"] as $group_ID) {
$ug = new UserGroup();
$ug->user_ID = $this->user_ID;
$ug->group_ID = $this->group_ID;
$ug->save();
}
parent::afterSave();
}
I just implemented what you want (Users table -> UserGroup table <- Groups table).Unfortunatelly I don't have the code in my home PC so I can get the code on Monday.
But the only thing you have to do is overide the afterSave function in your users model
Something like
protected function afterSave() {
//delete the user's relations in usergroup table
UserGroup::model()->deleteAll("user_ID =".$this->user_ID);
//get the new groups from the $_POST and save them to usergroup table
foreach ($_POST["groups"] as $group_ID) {
$ug = new UserGroup();
$ug->user_ID = $this->user_ID;
$ug->group_ID = $this->group_ID;
$ug->save();
}
parent::afterSave();
}
why don't you make use of db foreign constraints to delete related records automatically?