Place a list in a form and save associations between the two models.

Hi, I am new to Yii and would like your ideas on how to set up a list in a form.

I have a form to which I would like to add a list (like a CGridView) of items that are based on a different CActiveRecord model from the main form. My question is how is this placed in the form, or is it just another item on the page?

Eventually I want to be able to select multipliable items on this list (with a check-box or something) and add an association to the model in the main form. In the database there is a many to many association between these two models.

Finally, I would like to be able to change the order of the items in the list and then for the selected ones save that order to the database also.

How should this be set up to make the best use of tools provided by the Yii framework?

Thanks for your help.

Sorry, I don’t understand what you want. Can you explain it through an example?

Don’t forget that only one form can be sent from a web page (unless you use AJAX).

If you’re OK with a JS dependent page, then you could use CJuiSortable. It’s just a wrapper on JQueryUI Sortable. In each one of its sortable items, put a checkbox. It should amount to something like:




$items = array();

foreach ($selectableModels as $m) {

    // I suppose ($m->selected == 1) iff ($m is linked to the main object)

    $items[] = "<label>" . CHtml::checkbox('List[]', $m->selected, array('value' => $m->id)) . $m->name . "</label>";

}

$this->widget(

    'zii.widgets.jui.CJuiSortable',

    array('items' => $items)

);



When posted, the form will send in "List" an array of the selectd IDs.

Instead of saving this in a relational table, you may also consider serializing the list into a text field. There are pros (its easier to read and modify) and cons (you cannot search on the relations).

The form I have was generated by gii. It has about three text boxes for entering data or editing data for things like name and description. The model that this form was generated for is called JobType. So I put that form on my own controller and it works fine. I need to make it possible to link this JobType in the main form with multipliable entries of another type (in this case question). So I need to put a list of all the Questions in the form. How do I do that? Now the form will have more data then from just one model, but I should be able to take that returned data apart and save it in the correct place.

Thanks

Thanks François

I am confused about the $selectableModels. If I have a model named question how do I go about selecting the items in it. Also, can I use a CActiveDataProvider. I used them before and can do filtered selctions with them. Thanks

First, you should read the chapter of the official tutorial that explains forms, especially Collecting tabular input

Then, you should try to make your form work without any sorting. Just display the check boxes for the models linked to the main one. For instance, with the usual post and comments analogy, here is how you could deselect comments:




// add labels, html div, etc

echo $form->inputField($post, 'title');

foreach ($post->comments as $comment) {

    echo CHtml::checkbox("Comment[]", 1, array('value' => $comment->id));

}



Once your form works and your controller can add or delete relations between posts and comments, then you can add sorting like I suggested in my previous post.