CSort was not originally designed to work with RAR.
I think you can still give it a try, however.
You will need to specify its attributes property to set up a mapping between attribute aliases and attribute names (with prefix if necessary).
When calling link(), you need to provide a second parameter specifying the label because CSort won't be able to deduce the label based on a RAR attribute.
Columns Id and Project are sortable but i need the Project. prefix in the link call, otherwhise it creates strange URL parameters like sort=id:id when clicking a second time.
But it still doesn’t work with Customer column. I get this:
Quote
CDbCommand konnte das SQL-Statement nicht ausführen: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Customer.name' in 'order clause'
Nope, doesn't work. I tried three combinations: changing to ?? in controller only, in view only or in both, controller and view. The first two doesn't do anything. When changing it in both i get:
Quote
CDbCommand konnte das SQL-Statement nicht ausführen: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??.name LIMIT 10' at line 1
Column header has &sort=kunde as URL parameter. When clicking on Column Kunde (Customer), the SQL is:
SELECT `Projekt`.`id` AS t0_c0, `Projekt`.`kundeID` AS t0_c1, `Projekt`.`name` AS t0_c2, `Projekt`.`nummer` AS t0_c3, `Projekt`.`changedate` AS t0_c4, `Projekt`.`createdate` AS t0_c5, t1.`id` AS t1_c0, t1.`nummer` AS t1_c1, t1.`name` AS t1_c2, t1.`changedate` AS t1_c3, t1.`createdate` AS t1_c4 FROM `Projekt` LEFT OUTER JOIN `Kunde` t1 ON `Projekt`.`kundeID`=t1.`id` ORDER BY ??.name LIMIT 10
In your Project class, when you specify 'customer' relation, set the 'alias' option to be 'Customer' (otherwise AR will generate automatically a table alias). Then change all "??" reference to "Customer".
Now however the aliases are like 'user.username' instead of what is defined in user model attributeLabels() method. Is it possible to force it to use attributeLabels()?
The link method should use the alias you defined in $sort-attributes. I think attributeLabels() is for a different purpose and is not appropriate to be used as sort parameter names.
<?php
//controller list action
public function actionList() {
$criteria = new CDbCriteria;
$pages = new CPagination(User::model()->count());
$pages->pageSize = 25;
$pages->applyLimit($criteria);
$sort = new CSort('User');
$sort->attributes = array(
'user.username'=>'username',
'user.group_id'=>'group_id',
'user.email'=>'email',
'user.created'=>'created',
'user.email_confirmed'=>'email_confirmed',
);
$sort->applyOrder($criteria);
$users=User::model()->with('group')->findAll($criteria);
//This is for ajax, but I turned JS off before testing it. So this is not the problem
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('listPage', compact('users', 'pages', 'sort'));
else
$this->render('list', compact('users', 'pages', 'sort'));
}
//attributeLabels()
public function attributeLabels() {
return array(
'group_id'=>'User Level',
'email_confirmed'=>'Activated?',
'created'=>'Date Registered',
'email_visible'=>'Show Email',
);
}
//view code that is relevant
<th><?php echo $sort->link('username'); ?></th>
<th><?php echo $sort->link('group_id'); ?></th>
<th><?php echo $sort->link('email'); ?></th>
<th><?php echo $sort->link('created'); ?></th>
<th><?php echo $sort->link('email_confirmed'); ?></th>
What's happening:
The aliases are correctly pulled from attributeLabels() for the column headers.
When I click a header (for example username), it correctly sorts by it by generating the following url:
When I click the username column header again, it should sort by it again but descending instead. But it does not work correctly, and generates the following url:
<?php
public function link($attribute,$label=null,$htmlOptions=array())
{
$directions=$this->getDirections();
if(isset($directions[$attribute]))
{
$descending=!$directions[$attribute];
unset($directions[$attribute]);
}
In my instance it was looking to see if the 'username' key (the alias) was set in the $directions array, when it should have been looking for 'user.username'. It does not seem to be taking the aliases into account.
Something like this should fix it:
<?php
public function link($attribute,$label=null,$htmlOptions=array())
{
$aliases = array_flip($this->attributes);
if (isset($aliases[$attribute]))
$attribute = $aliases[$attribute];
$directions=$this->getDirections();
if(isset($directions[$attribute]))
{
$descending=!$directions[$attribute];
unset($directions[$attribute]);
}