Receiving Multiple Records For A Single Model From View In Post Method: To Save Parent Child Data

Hi,

I am in a situation where I have more than 4 child tables associated with one Parent table. I need to create a user experience in which user presses Save button only once, meaning by, user enters all the data in parent model fields, then enters data in all four child model fields and then presses the save button. As far as I know, having relations in the model allows you to make associated rows inserted easily but the main problem is how to receive multiple rows from view in POST method for a single model (here I essentially mean the child models). I have tried it manually by repeating the attributes of child model in view but when I save the record, only the last rowset gets stored in the child table along with parent table, one row for the child table gets missed. Kindly note that I am using CActiveForm and other Bootstrap widgets in my View files.

Is it possible in Yii or I am too wishful…any suggestions or comments ????

Many thanks in advance. Regards, Faisal

Hi Faisal,

I hope this section of the guide will help you.

Collecting Tabular Input

@FaisalKhan,

As Softark say you have to use Collecting Tabular Input.

You can also take a look @ my written wiki article

Thanks Kiran. I am trying it and will revert back with updates. However, one thing to share, for every single row that is sent from View to Controller in the POST, it has to be catered by separate model object (in your example you create two separate model objects for each address). Is there any way I can put all the rows in an array and send that array back to controller through POST method and then iterate on the model object…thanks for your support.

Regards

Faisal

Key Kiran,

Thanks man! your solution worked. I tested it with 100% desired results. I have mentioned all the details to this on another forum where I asked the same question. You can check my response there.

Thanks a lot.

Cheers :)

Faisal

@FaisalKhan,

I saw code looks fine, but haven’t test that… will test when got free time.

+1 for this on stackoverflow and here also :D