View For Assigning Existing Model To Another Model

hi, I need to create a view where I can manage related model/models for current model (some kind of CRUD but in one view). I have a model called Team and in "team/update" action I would like to manage team players (create new, assign already existing player etc.).

Now I’m displaying team players by CGridView which allows me to delete current players? But I also need to assign existing player models or even create new players in the same view.

What would be the best aproach to achieve this kind of functionality?

In general, there are a lot of ways to do such a things. So, each developer does it his own way. Don’t hesitate to do experiments.

One of the ways - take a look at CJuiDialog + AJAX.

I’ll describe only the idea, some example code could be viewed using links in the end of the post.

  1. For creation of new player you could use CJuiDialog (that would open by clicking some button named ie. ‘Create New Player’) that will load partial view of ‘createPlayer’ controller action loaded directly into CJuiDialog (view would consist of form controls needed for creation of player). Of course, action could be named something else.

  2. After hitting submit button submitted data is sended via AJAX to controller action, and in case of success CJuiDialog closed and CGridView is updated.

  3. In case of errors display errors. I recommend to use both client-side and server-side validations (luckily, most of validators are built-in into YII) to be sure that no un-needed data would be submitted to database.

  4. About assigning new players for the team - technique is similar. Add button named ie. ‘Assign player to the team’, display CJuiDialog that will do AJAX request to another controller - this action will return form with all needed form controls to assign new player to the team (possibly, dropdown select with list of players that could be assigned for current team). Again, after hitting the submit button, data is sended to assign action of some controller and if al was ok - update CGridView.

Hope you catch an idea, to help you here is a list of articles from yiiframework.com, take a look on it:

http://www.yiiframework.com/wiki/87/ajax-update

http://www.yiiframework.com/wiki/388/ajax-form-submiting-in-yii

http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii

http://www.yiiframework.com/wiki/49/update-content-in-ajax-with-renderpartial

http://www.yiiframework.com/wiki/72/cjuidialog-and-ajaxsubmitbutton