Relational sorting problem

Hey Guys,

I want sort by firtName not User_id, User_id is a foreign key. So how to sort by firtName?

public function relations()

{

return array(

‘user’ => array(self::BELONGS_TO, ‘User’, ‘User_id’),

);

}

Can’t understand what you’re trying to do here.

You can include a sort in a relation eg.




'users'=>array(self::HAS_MANY,'User','departmentId','order'=>'firstName ASC'),



Actually I want to sort by firstName. Here is my view file

    array(


    'name'=>'User_id',


    'value'=>'$data->user->firstName',


    ),

if I use HAS_MANY then I got this error

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘user.User_id’ in ‘where clause’

I dont understand with what table you’re trying to relate?

I have two table

table 1. (User)

id

firstName

lastName

address

table 2. (Food)

id

User_id

food

in table 1 model

‘table2’ => array(self::HAS_MANY, ‘Food’, ‘User_id’),

in table 2(Food) model

‘user’ => array(self::BELONGS_TO, ‘User’, ‘User_id’),

In the view page, I want to sort by firtName, not User_id, but sorting by firstName is not working. Have any Idea where is the problem?

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

'id'=&gt;'records-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'columns'=&gt;array(


	


	


array(


    'name'=&gt;'User_id',


    'value'=&gt;'&#036;data-&gt;user-&gt;firstName',


    ),


   'food',





),

)); ?>