Save The Records More Than Ten Models..


i need to save the records more than in ten tables.

which one is best? model->save() or using procedure …

Please help me …

If you want to save multiple models at once, try this:

Collecting Tabular Input

Hi dhanakumar,

Can you please elaborate what you are trying to achieve?

Do you have a form with data from 10 models and want post it at once?


Hi leandro,

i have a form when i was post the data,it will be stored in ten different tables.

for eg:

Employee master

fields like Emp Name,Emp address1,Emp address2,Emp address3 … etc

i need to stored Emp address1,Emp address2,Emp address3 in emp_master table.

after that address stored in address master,get last insert id then stored emp master table with address id …

can you please help me …

Hi dhanakumar,

First of all, could be important review the tables schema design to ensure that you really needs 10 tables to store that information.

Usually, address fields could be added to some "person" o "employee" table:

Table employee








… etc

Second, if you are trying to store several different addresses for some employee (I can’t understand why) and you really needs a separate “address” table, the simplest way to solve this is use separate pages for each table (employee and address. In one page the user enter the basic employee data and save the model to the employee table. Then, add a link like “Add address” and redirect the user to a second page to enter that data (once you saved the new address, could return to the employee page). In the employee table you could use CDetailView to show the employee data and a CGridView to show the basic fields of the several added addresses.

Third, if you really wants save both models at the same time (in the same page) you could do that also using an approach similar to this:

By the way, you mention that you have more than 10 tables to save at the same time. I see employee and address data (and as I said, I think both kind of data could be stored in the same table). Could you please mention what are the other tables? Are them related to the employee model?



Hi leandro,


and one more questions… ?

Now we are using mysql db.

so mostly used stored procedure for more than one five table insert and select…

Is this correct way … ?

and some problems using stored procedure,it cannot through the exceptions.

Hi dhanakumar,

The preferred method to handle data in Yii is ActiveRecord models (

ActiveRecord models provides a good integration with the entire Yii framework.

The ActiveRecord classes uses tables as their data source, not stored procedures.

Of course, if you have some legacy stored procedure, you can invoke it:

$command = Yii::app()->db->createCommand('call procedure_name("args")');


But, if don’t recommend use stored procedures for simple CRUD operations.

Instead, first create your tables, then use Gii to automatically create the models and use models as much as possible.



Hi leandro,

i am using same as below method,


$command = Yii::app()->db->createCommand(‘call procedure_name(“args”)’); $command->execute();[/sub]

but if any exception through in procedure i cannot see the error and line number directly.

if i use the normal model i can view the error with line … but it is not when used procedure.

One more problem if i use the select procedure,cannot sort and search the columns in CGridview …