Access Multiple Models from the one view/controller

Hi All,

i am new to Yii and want to create a view from which i could fell multiple models.

there are 2 tables, Table A, Table B

Table A is the main table and B is the a supporting table which keeps some extending information of table A.

If user want to fill table B, he should be able to do it the view page of Table/Model A other wise default would be


Now i have no idea how to fill table/model B using A’s MVC.

Could anyone give me some idea how to achieve that in yii…?

waiting for the reply…!:)



Check this: How to use a single form to collect data for two or more models?

And welcome to Yii!!!

Thanks PoL,This helped me a lot. :)


facing a small problem. lets take an example

now the following code is supposed to save b only if a is saved after validation


… if($valid)




    $b->a_id = $a->id;





$this->render(‘create’, array( ‘a’=>$a, ‘b’=>$b, )); }

now suppose if $a is not validated and eventually not saved due to the "required" rule on some of its field then what happens to $b…?

In my case, its giving me error on the view that “it can’t convert the “b” to the string”.

how to solve this problem

-Big O

Edit: just resized text… all text was too big…[/size]

This error happens to me when I do try to render $b misstyping the object’s some property and concatenating to a string ( ie. echo $b , echo ‘some string’.$b ) can we see the actual code?

Hi Antonio,

i am not allowed to share the code

but what i can do is share a prototype of that

this is the ModelBController’s code

ModelA can have Many ModelBs but there relation is saved in ABRelationshipModel. so there is none of ModelA’s primary key is saved in ModelB as foreign key.

ModelA’s and ModelB’s, primary key of both is saved in ABRelationshipModel.

There is direct relation ship between ModelB and ModelC. and these are the models in my actual question, giving me the problem

public function actionCreate()


	$model=new ModelB;

        $modelABR = new ABRelationshipModel;

        $modelC = new ModelC;

        //we need to save the modelA and ModelB RelationShip

        //_a_Obj is filled by the filterAContext to ensure right A model

        // is loaded before the ModelB is created

        $modelABR->a_id = $this->_aObj->a_id;

        $a_id = $modelABR->a_id;

	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model);

	if(isset($_POST['ModelB'], $_POST['ModelC']))



            	$model->changed = new CDbExpression('NOW()');

	            $modelC->attributes = $_POST['ModelC'];



	                $modelABR->ModelB_id = $model->ModelB_id;

	                $modelC->ModelB_id = $model->ModelB_id;

	                $flag = true;

	                if( $modelABR->save())


	                    if( !$modelC->save())




	                        $flag = false;





	                    $flag = false;



        		     if ($flag == true)



                               array('view', 'q_id'=>$a_id,










Hope this will clear the question.!

-Big O

solved the issue…

i was doing

<?php echo $form->errorSummary( $model, $modelC) instead of

<?php echo $form->errorSummary( array($model, $modelC)) //<<----- was not using array();

in _form.php

Thanks for listening to me

-Big O

Hi Big O,

Why dont you set the $b to null if A is not validated and check that value against null on your view? This way a value is passed and then on the view? As I think the problem arises there.

Please update me.

Hi Antonio,

I have already resolved the issue…

and mentioned it how i did that…!


-Big O

wow! Right at the same time (check it in your post)

lol, I was probably writing whilst you were posting your answer.

Great man… thanks!

Thanks, the same was happening to me right now, array was missing.

See you, Pablo.