user1200383
user1200383

Reputation: 43

yii 'through' relation search

I have the following tables.

User:
---------
id<br>
firstName

Project:
---------
id<br>
Name

StaffingManager
---------------
id<br>
User_id(FK)<br>
Total_Staff<br>

StaffingProjectMonth
-----------------------
id<br>
Project_id(FK)<br>
StaffingManager_id(FK)<br>

I want to define the relations in StaffingProjectMonth model

This is the default relation defined by YII using gii

public function relations()
    {
        return array(
            'project' => array(self::BELONGS_TO, 'Project', 'Project_id'),
            'staffingManager' => array(self::BELONGS_TO, 'StaffingManager', 'StaffingManager_id'),
        );
    }

I was able to get the ProjectName and search by ProjectName.

I want to get the UserfirstName and search by that.

I defined the relation this way.

return array(
            'project' => array(self::BELONGS_TO, 'Project', 'Project_id'),

            'staffingmanager' => array(self::BELONGS_TO, 'StaffingManager', 'StaffingManager_id'),

            'user'=> array(self::HAS_MANY,'User',array('User_id'=>'id'),'through'=>'staffingmanager' ),
        );

and in search method I did this:

$criteria->with = array('project','user');
//$criteria->compare('id',$this->id);
$criteria->compare('Name',$this->Project_id,true);
$criteria->compare('firstName',$this->StaffingManager_id,true);

and in the view:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'staffing-project-month-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(       
    //  'id',       
        array('name'=>'Project_id','header'=>'Project','value'=>'$data->project->Name',),
        array('name'=>'StaffingManager_id','header'=>'User','value'=>'$data->staffingmanager->user->firstName',),
        ..............  

only the search by project name works. Able to sess the UserfirstName but unable to search by the firstName. Some wrong in defining relations.

Any help is appreciated. Thanks.

Upvotes: 2

Views: 3772

Answers (2)

gekh
gekh

Reputation: 2122

I had troubles with $criteria->with and ”through“ relation. Thing was in relation kind. BELONGS_TO doesn't work correct. I switched it to HAS_ONE and inverted id direction — array('parent_id'=>'id'). Now it's working!

Upvotes: 0

c-cba
c-cba

Reputation: 136

I recommend the following excellent wiki article by redguy, that describes how to search by related model attributes:

Following the approach of this article:

You should declare two new variables in your model:

public $project_name;
public $staffingmanager_firstname;

You should declare these new variables as safe for search in the rules() method:

array( 'project_name,staffingmanager_firstname,...', 'safe', 'on'=>'search' ),

Your search() method criteria should look like this:

$criteria->compare('project.Name',$this->project_name,true);
$criteria->compare('user.firstName',$this->staffingmanager_firstname,true);

In your view file, the 'columns' should be:

array('name'=>'project_name','header'=>'Project','value'=>'$data->project->Name',),     
array('name'=>'staffingmanager_firstname','header'=>'User','value'=>'$data->user->firstName',),

Hope this helps.

Best regards...

Upvotes: 2

Related Questions