Using CGridView from another model

Hi everybody,

I created two models, the Profiles and EmergencyContacts, and I generated their view files both with the use of Gii. I have to include the ‘admin’ view file from EmergencyContacts in the ‘update’ view file of Profiles , so from the ‘update’ file I called ‘admin’ like this:




<?php

//'update' from Profiles 

//calling 'admin' from EmergencyContacts

$criteria=new CDbCriteria;

$criteria->condition='user_id='.$model->user_id;

echo $this->renderPartial('/emergencyContacts/admin', array('model'=>  EmergencyContacts::model()->findAll($criteria),'id'=>$model->user_id,));

?>



But it’s producing an error. How can I accomplish such task?

Please help.

First check the API for renderPartial

Your code should be




<?php

$this->renderPartial('/emergencyContacts/admin', array('model'=>  EmergencyContacts::model()->findAll($criteria),'id'=>$model->user_id,));

?>



OR




<?php

echo $this->renderPartial('/emergencyContacts/admin', array('model'=>  EmergencyContacts::model()->findAll($criteria),'id'=>$model->user_id,),true);

?>



Then what is the error you get?

Thanks for the reply.

Yep, I assure you I did my part of researching. In fact, the code I got now is from a post I got somewhere in this forum. But thanks for reminding me, I saw the mistake in my line of code :)

It returns the Fatal error: Call to a member function search() on a non-object error. Actually, the same error I am getting whenever I include the criteria in the model im passing. When I do just this:




//removed the criteria

echo $this->renderPartial('/emergencyContacts/admin', array('model'=>  EmergencyContacts::model(),'id'=>$model->user_id,),true);



the error is gone and the grid shows up.

But…

it displays ALL the record, even the one that does not belong to the profile. If I can’t put the criteria in my parameter, where should I place it?

I think you cut from your code some important piece of information ;D… findAll(…) ?

It should be




echo $this->renderPartial(

        '/emergencyContacts/admin',

        array(

            'model' => EmergencyContacts::model()->findAll(

                    'user_id=:uid',

                    array(

                        ':uid'=>$model->user_id,

                    )

            ),

        ),

        true

);



By the way, you seem to have a relation ‘Profile’ HAS_MANY ‘EmergencyContacts’, you could define it and it would save you some code.

In Profile.php do this:




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(

			'emergencyContacts' => array(self::HAS_MANY, 'EmergencyContacts', 'user_id'),

		);

	}



Then you can load all emergency contacts in your script by simply doing




echo $this->renderPartial(

        '/emergencyContacts/admin',

        array(

            'model' => $model->emergencyContacts,

        ),

        true

);



You can specify the relation the other way as well (using BELONGS_TO).

Use a code formatter and auto completion, you would see quicker your mistakes. A model name should not be plural. That’s for the best practices ;)

Thanks again for the quick reply. Impressive :)

I tried this, unfortunately it still renders the Fatal error: Call to a member function search() error. I did a var_dump on the two methods(one with findAll and one just EmergencyContacts::model(),which is not producing any error), and found out that the first one returns an array, and the latter an object. Probably the cause of the error is that we are passing an array when it should be an object?

Exacta, these are what I got (generated when I created the model) :)

Code formatter: check!

Auto completion: check!

Model name should be singular: fail! :D

Will consider your advise on the next project. :)

Btw,




echo $this->renderPartial(

        '/emergencyContacts/admin',

        array(

            'model' => $model->emergencyContacts,

        ),

        true

);



Did not work either, and produced the error Property "Profiles.id" is not defined.

Wierd… :unsure:

EmergencyContacts::find(/blabla/) would return an object, EmergencyContacts::findAll(/blabla/) an array… everything is normal from that point of view :lol:.

EmergencyContacts::model() returns the static model of your class not an instance of EmergencyContacts, it doesn’t carry any data and it doesn’t look like you should be using it :unsure:

In fact what are you doing with variable $model in view ‘/emergencyContacts/admin’? Should you not be using a CActiveDataProvider instead of an array of active records?

Do you suggest to use CArrayDataProvider instead of CActiveDataProvider? To be honest, I decided to use the latter because it is what I’m comfortable in using, until I encountered this problem. I don’t know how to use CArrayDataProvider with CGridView’s filter and search too :(

I’m open for any suggestions, I’m a Yii toddler anyway. I’ll start searching for alternative now. :sigh:

Ahmmm…would you be kind enough to suggest a workaround that I can do to accomplish my task?

Thank you for your time. :)

Stupid me, your view is a ‘admin’ gii generated view. In fact this view does not fit what your are trying to do (search parameters are generated by a form inside ‘admin’ view which is by default blank unless GET paramters…)

Do this (instead of renderPartial)




<?php

$dataProvider = new CActiveDataProvider(

        'EmergencyContacts',

        array(

            'criteria' => array(

                'condition' => 'user_id=:uid',

                'parameters' => array(

                    ':uid'=>$model->user_id

                )

            ),

        )

);

$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'emergency-contacts-grid',

    'dataProvider' => $dataProvider,

));

?>



Thanks teher, it was able to display only the needed records now…I have to disable filtering, but I can live with that :)

Thanks again :)

This:

$emcontactsmodel = EmergencyContacts::model()->findAll($criteria);

Does not return a model, it returns information from a model, and usualy an array.

This:

$emcontactsmodel = EmergencyContacts::model();

Returns a model.