GSTAR
(Omzy83)
August 5, 2010, 8:10pm
1
I have a CGridView that represents data from model Enquiry. I have the following column which is meant to display a relational attribute:
array(
'name'=>'accepted_at',
'value'=>'$data->QuoteAcceptedAtDate',
),
‘accepted_at’ is a field in model Quote, but in fact I can type whatever I want for ‘name’ and it just transforms it into the header text. The value is being displayed correctly.
I assume this is why the sort isn’t working for that column, because it isn’t referencing the actual field.
Here is the relation in the Enquiry model:
'accepted_quote'=>array(self::BELONGS_TO, 'Quote', 'accepted_quote_id'),
I also put in $criteria->with=array(‘accepted_quote’); in my search() function in the Enquiry model.
ccorreia
(Claudio Correia82)
August 6, 2010, 4:48pm
3
This is what I’ve done in my code to make this work:
'name' => 'accepted_at',
'value'=>'Quote::model()->FindByPk($data->accepted_quote_id)->accepted_at',
This is working for me, maybe you have to adjust to your particular case.
Also if you using the with keyword on the criteria that gives your dataProvider, you should use (in your case)
'name' => 'accepted_quote.accept_at'
It will use the correct value but it won’t be sortable, this requires something else that I quite haven’t understand myself on how to do it
But in the first example you’ll be able to sort by that column, maybe beeing a bit heavier on performance.
Hope it helps.
GSTAR
(Omzy83)
August 6, 2010, 5:14pm
4
Sorry but that does not work either, in fact that does exactly the same thing as my code does, only difference is I use a function to call the Quote model and yours is making a call direct from the CGridView.
As I say the correct value is already being displayed, just the column isn’t sortable.
GSTAR
(Omzy83)
August 10, 2010, 11:22am
5
Anyone else able to advise?
GSTAR
(Omzy83)
August 23, 2010, 1:05pm
6
BUMP. Anyone know how to make this column sortable please?
mdomba
(Maurizio Domba Cerin)
August 23, 2010, 1:09pm
7
GSTAR
(Omzy83)
August 23, 2010, 1:26pm
8
I have tried doing it like this:
$criteria->with=array('accepted_quote');
$sort=new CSort();
$sort->attributes = array('*', 'accepted_at');
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'sort'=>$sort,
));
But I now get a javascript alert message saying:
"Column not found: 1054 Unknown column 't.accepted_at' in 'order clause'
GSTAR
(Omzy83)
August 24, 2010, 1:29pm
9
BUMP. Anyone able to advise?
mdomba
(Maurizio Domba Cerin)
August 24, 2010, 1:50pm
10
GSTAR
(Omzy83)
August 24, 2010, 1:53pm
11
Yes bro I checked it, as per my code above.
I specified the ‘with’ condition to load the relation
I created the CSort object and specified all attributes as well as the relational attribute
What else is missing?
mdomba
(Maurizio Domba Cerin)
August 24, 2010, 2:01pm
12
But your sort attributes is not specified like that on the link… I will just copy/paste the sort attributes from that link…
$sort->attributes = array(
'customer'=>array(
'asc'=>'customer.customer_name',
'desc'=>'customer.customer_name desc',
),
'school'=>array(
'asc'=>'school.school_name',
'desc'=>'school.school_name desc',
),
'submitted_date',
'requested_date',
);
‘customer’ and ‘school’ in this example are the column names for CGridView…
GSTAR
(Omzy83)
August 24, 2010, 2:22pm
13
Thanks, this has worked for me:
$sort->attributes = array(
'*',
'accepted_at'=>array(
'asc'=>'accepted_at', 'desc'=>'accepted_at DESC'
)
);