Save HAS_MANY relation with model class

Hello Everyone,

Any body can suggest to save a has many relation in yii.Since, i have seen some forum post which are suggesting to use extensions for it.But surely there will be any other solution for it.:)

I hope you guys can suggest me it.

Simply i have two relational table.

Table A has Many relation in Table B.

So, i have just define a relation in Model class of Table A.

public function relations()


		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'records'=>array(self::HAS_MANY, 'Table_A','id'),



And in My controller action code i am just using a


To save this relational values.But What i am getting is here Table A record is created perfectly But There is n any record available in Table B :(

Might be a silly mistake i am doing here.Anybody who can suggest me how to save a HAS_MANY relation with Yii and without help of any extension.

Thank in Advance.!! :)

Yes, there’s no magic in it. It’s quite simple.

You just have to loop through the related models to set the FK and save.

    // save model A


    // save related models

    foreach($model_bs as $model_b ) {

        $model_b->model_a_id = $model_a->id; // set the foreign key



But the difficult part is not the saving of them, but the gathering inputs of those related models with a form.

Sometimes you have to deal with one instance of model_a and multiple instances of model_b in a single form … not necessarily so, but sometimes it’s preferable for the user friendliness.

The starting point would be the following section of the guide.

Collecting Tabular Input


softark, I know about this processs which called Collecting tabuler formate.But i was thinking a bit different Here.Since In CakePHP i have used a HAS_MANY relation and define a proper syntax for it in model Class.Then Calling a $model_a->save(); in the action save this relation and also it works like a charm On updating and Deleting operation. :)

But Why this usefull feature is still not included In Yii.:( I am going to send a feature request for it.

What you think about it softark ?

Anyways thanks for your quick response. :)

And you can check what i am expecting in Yii which is already present in Cakephp.

Check This link Which show that this kind or relational active records works like a charm in Cakephp but still not available in Yii :(

I have just posted a Feature request post at this link.

Waiting for some good suggestions :)


Yeah, I would also love to see a feature like that built in the core.

I don’t know why they didn’t do it so far. But the developers might have noticed some critical drawbacks in such things as a built-in feature of the core. I really don’t know.

Yuppp, :)

Lets make a request to Yii Dev staff to include this core feature in next version of Yii. ;)

I have just started a Thread here ;)

BTW, I didn’t have a chance to use this kind of extension so far.

But I’d like to give a try to “with-related-behavior” written by creocoder.

There’s a recommendation from Da:Sourcerer.

Sounds promising to me. :)

Thanks softark,

For your suggestions.i’ll check these extensions:)

i’m new to yii…am using these has_many relation to save data to database …while i search the result i found ur answer useful for me i think.but i had one doubt in this tabular input method…my question is that…the method getItemsToUpdate();i wrote in model file thst’s right?..but how i write the method i don’t have idea about it…so plz help me out to solve my issuses…

Hi MohammedNizamudeen,

As "$this->" indicates, "getItemsToUpdate" method belongs to the same class as "actionBatchUpdate" method … it belongs to the controller class, not to the model class.

You may want to show a variable number of models, and eventually you would want to update the existing ones. But I think you’d be better start simple with a creation of a fixed number of the models.

public function getItemsToUpdate()


    $items = array();

    for ($i = 0; $i < 5; $i++) {

        $items[] = new Item();


    return $items;