M
M
Max Khartanovych2015-05-29 17:40:01
Yii
Max Khartanovych, 2015-05-29 17:40:01

Yii 1.1 alias support in cSort?

Tell me, is it possible to add alias support to the CSort class in Yii 1.1 latest version?
Example:

// Объявляем критерию
$crt = new CDbCriteria();
$crt->alias = 'communityPosts'; // Устанавливаем alias с которым будем работать
        if ($filter) {
            if ($community->isMember()) {
                $crt->scopes = array('pending', 'declined' => array('operator' => 'OR'), 'activeUser');
            } elseif ($community->isOwner()) {
                $crt->scopes = 'pending';
            }
        } else {
            $crt->scopes = 'approved';
        }



$sort = new CSort('Post');
$sort->defaultOrder = 'id DESC'; 
// Мне нужно использовать alias критерии который я установил ранее, что бы не писать "$crt->alias . '.id DESC'"
$sort->attributes = array(
            'declined' => array(
                'desc' => 'status DESC, id DESC',
            ),
            'popular' => array(
                'asc' => 'status',
                'desc' => 'status DESC',
            ),
        );
$sort->applyOrder($crt);

// Получаем записи
$posts = $community->posts($crt);

There is a suggestion to slightly modify the CSort::applyOrder() method
// До
$criteria->order.=$order;
// После
$criteria->order.=$criteria->alias.'.'.$order;

Of course, this is a simple option, it would be better to add a separate setter method ... but in general, what's the idea?
UPD: I looked into the CSort::getOrderBy() method and saw
$attribute=($criteria===null || $criteria->alias===null ? $this->getModel($this->modelClass)->getTableAlias(true) : $schema->quoteTableName($criteria->alias)).'.'.$schema->quoteColumnName($attribute);

I think the issue is closed and I'm probably doing something wrong

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
olegf13, 2015-06-15
@olegf13

If I understand your question correctly, then you just need to carefully read the annotation to the CSort::attributes property here - www.yiiframework.com/doc/api/1.1/CSort#attributes-... .
Especially the part that starts with " Starting from version 1.1.3 ..."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question