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?
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?
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
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)
EmergencyContacts::find(/blabla/) would return an object, EmergencyContacts::findAll(/blabla/) an array… everything is normal from that point of view .
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
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?
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…)