Вопрос

i have a table that one of column has value from function

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'camera-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    array(
        'name'=>'location', 
        'header'=>'Location', 
        'value'=>'$data->location->name'
    ),
    array(
        'header'=>'Last Date',
        'value'=>'$data->myfunction($data->location->name)'
    ),
),)); ?>

my questions is:

  1. how to sort column Last Date?
  2. there's chance the Last Date is null. So, if i want to display rows with Last Date is not null, what i'm suppose to do?

Thanks before :)

Это было полезно?

Решение 3

in the end, i found this solution. overall, i must make my custom variable as SQL query. So, i can sort or search custom variable like other variable from database.

in mymodel,

  1. declare new variable public $LAST_DATE;

  2. add $criteria

$criteria->select = array('*','COALESCE(location.name, \'\') AS LAST_DATE'); $criteria->order = 'LAST_DATE DESC'; return new CActiveDataProvider($this, array( 'criteria'=>$criteria, 'sort'=>array( 'defaultOrder'=>'LAST_DATE DESC', 'attributes' => array( 'LAST_DATE'=>array( 'asc'=>'COALESCE(location.name, \'\')', 'desc'=>'COALESCE(location.name, \'\') DESC', ), '*', ), ),

Другие советы

Sorting will look like this:

    $sort = new CSort;
    $criteria->with = array(
        'location'
    );

    /* Sort criteria */
    $sort->attributes = array(
        'col_name'=>array(
            'asc'=>"location.date asc",
            'desc'=>"location.date desc",
        ),
        '*',
    );

    /* Default Sort Order*/
    $sort->defaultOrder= array(
        'col_name'=>CSort::SORT_ASC,
    );

    return new CActiveDataProvider($this, array(
        'pagination'=>array('pageSize'=>20),
        'criteria'=>$criteria,
        'sort'=>$sort,
    ));

And NULL you need to check in the row value:

  'value'=>'$data->location ? $data->myfunction($data->location->name) : ""'

If you don't have any column in database that you can order by, you should select all record from database, then sort them manually and put into CArrayDataProvider.

As @Alex said the only solution, without editing the database, is to read ALL the records from the database, apply your function to each model and then sort them manually. Not only will this be a memory hog for a large table, it is also terribly inefficient since a) you will discard most of the records from the database if you are using pagination and b) your custom sorting method will most likely be slower than your database's.

From the information you've given, my solution would be to add an additional field to your table and set it to the result of $data->myfunction($data->location->name)'. A CActiveDataProvider with defaultOrder set can the be used to feed the CGridView.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top