Model criteria without as


i use a select criteria.


$this->_criteria = new CDbCriteria();

$this->_criteria->select = ', t.cim, (t.nap_'.$nowday.'_0) as start_time, (t.nap_'.$nowday.'_1) as end_time, t.telszam,, t.kid, ifnull(k.kid, 1000) as sorrend';

$this->_criteria->order = 'sorrend asc, asc';

$this->_criteria->join = 'LEFT JOIN tbl_kiemeles_fizetve k ON (';

if(isset($_POST['example'])) {

$this->_examplecriteria = new CDbCriteria();

$this->_examplecriteria->condition = 'example = 1';


                        'with' => array(

                            'hirdetesextra' => array(

                                'select' => false,

                                'alias' => 'e',

                                'joinType' => 'INNER JOIN',






If the $_POST[‘example’] empty, the select is great, but is it not empty the model generate alias to the field.

How to "disable" the AS?

SELECT `t`.`id` AS `t0_c0`, `t`.`cim` AS `t0_c1`, (t.nap_5_0) as start_time, (t.nap_5_1) as end_time, `t`.`telszam` AS `t0_c59`, `t`.`pid` AS `t0_c2`, `t`.`kid` AS `t0_c3`, `t`.`kid` AS `t0_c3`, 1000) as sorrend FROM `tbl_hirdetes` `t` LEFT JOIN tbl_kiemeles_fizetve k ON ( INNER JOIN `tbl_hirdetes_extra` `e` ON (`e`.`hirdetes_id`=`t`.`id`) WHERE example = 1 ORDER BY sorrend asc, asc

You could simply use ‘if else’.

if (isset($_POST['example'])) {

    $this->_criteria->select = '...';

} else {

    $this->_criteria->select = '...';


FYI, CDbCriteria::select can be an array.

So, you can make it more comprehensive by defining select using an array.

$this->_criteria->select = array(





if (something) {

    $this->_criteria->select[] = ' as yyy';

} else {

    $this->_criteria->select[] = 't.yyy';


Yes, i use this. But is it too lot code if i have 40-50 variable and mysql field.

Um, maybe yes.

If I have too many if else in a single method, I may try to refactor it.

In your case, I might have created the dedicated two methods, one for ‘sample’ and the other for ‘non-sample’, to keep things simple.