Yii Framework Forum: Searching With Multiple Models. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Searching With Multiple Models. Rate Topic: -----

#1 User is offline   TNC 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-January 13

Posted 28 February 2013 - 08:40 PM

Hello there, i have been through different pages and different topics to meet my specification, and finally i post here.
What i am trying to do is,
I have a UserProfile and User model.

Entities/Attributes.
------------------------
UserProfile: id,user_id,first_name, last_name,...
User: username,id


I want to show all entities form UserProfile and User in cgridview and able to search them separately.
so i created a controller action like this.
public function actionControllerAction(){
$model = new UserProfile('search');
		$model -> unsetAttributes();
		// clear any default values
		if (isset($_GET['UserProfile']))
			$model -> attributes = $_GET['UserProfile'];
		
		$this -> render('MyView', array('model' => $model ));
}
// CGridView
 $this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'user-grid',
		'dataProvider'=>$model->search(),
 		'filter'=>$model,
		'columns'=>array(
				
			  array('name'=>'first_name',
				'header'=>"First Name",
				'htmlOptions'=>array('style'=>'text-align: center'),),
				
			    array('name'=>'user_id',
				'header'=>User::model()->getAttributeLabel('username'),
				'value' =>'$data->getRelated(\'user\')->username',
				'type'=>'raw',
                 ....................

// my Model search function:
$criteria -> compare('first_name', $this -> first_name, true);
$criteria -> compare('user_id', $this->user_id, true);
return new CActiveDataProvider($this, array('criteria' => $criteria, ));


But i am not able to search by username, how can i acheive this?? Help needed !
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 02 March 2013 - 02:08 AM

Hi TNC,

So your User and UserProfile is in a typical relation of "HAS_ONE" and "BELONGS_TO".
User HAS_ONE UserProfile
UserProfile BELONGS_TO User

Do you have those relations already established in your model codes? I guess you have.
Then, UserProfile.php could be like this:
...
public $username;
...
public function search()
{
    ...
    $criteria->with = array('user');
    ...
    $criteria->compare('first_name', $this->first_name, true);
    $criteria->compare('user_id', $this->user_id, true);
    ...
    $criteria->compare('user.username', $this->username;
    ...
    return new CActiveDataProvider($this, array('criteria' => $criteria, ));
}


See the details in the guide:
http://www.yiiframew...en/database.arr
Or you may want to read this wiki article first.
http://www.yiiframew...el-in-cgridview
0

#3 User is offline   TNC 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-January 13

Posted 03 March 2013 - 09:39 AM

Thanks for the reply, and the answer ! I should have
 compare->with ''
part in my model search function. Got it.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users