1

I have one table like:

name
startDate
endDate
units
price

and I show them in a gridview for a given year as follows (index.php in views):

$gridColumns = [
    'name',
    [
        'attribute' => 'january',
        'format' => 'currency',
        'value' => function ($data) {
            if(date("m", strtotime($data["startDate"])) == 1) {
                return ($data["price"]*$data["units"]);
            }
            else {
                return "";
            }
        },
        'options' => ['style' => 'max-width:20px;'],
        'pageSummary' => true,
    ],
    [
        'attribute' => 'february',
        'format' => 'currency',
        'value' => function ($data) {
            if(date("m", strtotime($data["startDate"])) == 2) {
                return ($data["price"]*$data["units"]);
            }
            else {
                return "";
            }
        },
        'pageSummary' => true,
    ],
    ... until month 12
];

In my search model i have defined:

public $january, $february, $march, $april, $may, $june, $july, $august, $september, $october, $november, $december;

placed them as safe in rules and defining attributes in order to get grid header with sortable items

$dataProvider = new ActiveDataProvider([
    'query' => $query,
        'pagination' => [
            'pageSize' => 0,
        ],
    'sort' => [
        'defaultOrder' => [
            'name' => SORT_ASC,
        ],
        'attributes' => [
            'name',
            'units',
            'price',
            'january',
            'february',
            'march',
            'april',
            'may',
            'june',
            'july',
            'august',
            'september',
            'october',
            'november',
            'december',
        ],
    ],
]);

but when i try to sort by one of the months it gives an error like:

exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'january' in 'order clause''

How could I sort by these calculated columns that doesnt exist in the database?

Thanks

farrusete
  • 435
  • 9
  • 24
  • 2
    where are you calculating them and how are you adding them to the `select` part of the $query? – csminb May 28 '17 at 16:56
  • Possible duplicate of [MySQL sort on a calculation](https://stackoverflow.com/questions/2369624/mysql-sort-on-a-calculation) – SiZE May 29 '17 at 13:06

0 Answers0