Jump to content
Larry Ullman's Book Forums

Yii Tutorial - Basic Controller Edits In Yii: Relations


mcmurphy510
 Share

Recommended Posts

Hi,
 
I got all the way through the tutorial with no problems until the very end.
 
In “EmployeeController.php” I’m trying to get yii to display the relation for department rather than the ID number. I have tried this code in the loadModel method:

$model=Employee::model()->with('department')->findByPk((int)$id);

I have double checked all of my relations and they all seem to be in order.

 

It’s still showing the ID number rather than the department name.
 

Any ideas?

 

Thanks

Link to comment
Share on other sites

Thanks for the reply Larry.
 
Here are my relations:
From Employee.php:

'department' => array(self::BELONGS_TO, 'Department', 'departmentId')

From Department.php:

'employees' => array(self::HAS_MANY, 'Employee', 'departmentId')

 
It looks like my problem was in the view code.  For employee/admin.php I had:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'employee-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'departmentId',
        'firstName',
        'lastName',
        'email',
        'ext',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));

Which I have replaced with:

    ...
    'columns'=>array(
        'id',
        'department.name',
        ...

Is this the best way of going about this?

It is now showing the department name, but it's showing the column name as "Name" rather than Department and search functionality is also gone for that column.

 

Here's a screen cap of what it's doing:

1heni.jpeg

Thanks again.

Link to comment
Share on other sites

Update:
 
If I change the attributeLables() method in Department.php from: 

public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'name' => 'Name',
    );
}

To:

public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'name' => 'Department',
    );
}

"Name" is now shown as "Department"

 

I still do not have search on that column.

 

Thanks.

Link to comment
Share on other sites

Thanks Larry,

 

Sorry for the reply delay.  Other projects got in the way.

 

I've been evaluating ZF2, Codeigniter and Yii for our SaaS app.  So far, Yii looks like the winner.  So I probably will be buying the book, but for now if you could post the code it would be much appreciated.

 

Thanks again.

Link to comment
Share on other sites

Sorry for the delay. So the code I have actually comes from "The Yii Book" and is a bit long and for a different example. But I'll post it here and it should explain it enough for you to use it. I'm going to post all of this on my blog (maybe on Monday), too. Here's the excerpt...

 

 

Two steps are required to solve this riddle. First, a text input must be displayed for the column. The filters are based upon the model, and as the model has no `pageUser.username` attribute, no text input shows. The fix for that is to name the column `user_id` for the filters, but still use `pageUser.username` as the value of the column:

 

```php

# protected/view/page/admin.php

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

'id'=>'page-grid',

'dataProvider'=>$model->search(),

'filter'=>$model,

'columns'=>array(

'id',

array (

'header' => 'Author',

'name' => 'user_id',

'value' => '$data->pageUser->username'

),

// And so on.

```

 

Now the grid shows an input for the column.

 

Next, the `search()` method must be changed so that the query uses the supplied `user_id` value to compare against the `username` column in the `user` table.

 

The default `Page::search()` method looks like this:

 

```php

# protected/models/Page.php

public function search() {

$criteria=new CDbCriteria;

$criteria->compare('id',$this->id,true);

$criteria->compare('user_id',$this->user_id,true);

// Other comparisons

return new CActiveDataProvider($this, array(

'criteria'=>$criteria,

));

}

```

 

First, you'll want to add a `with` clause to change from lazy loading of the user records to eager loading. Then, change the comparison to use the `pageUser.username` field instead of `user_id`:

 

```php

# protected/models/Page.php

public function search() {

$criteria=new CDbCriteria;

$criteria->with = 'pageUser';

$criteria->compare('id',$this->id);

$criteria->compare('pageUser.username',

$this->user_id,true);

// Other comparisons

return new CActiveDataProvider($this, array(

'criteria'=>$criteria,

));

}

```

Link to comment
Share on other sites

 Share

×
×
  • Create New...