Model criteria without as

Hi,

i use a select criteria.

Example:




$this->_criteria = new CDbCriteria();


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

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

$this->_criteria->join = 'LEFT JOIN tbl_kiemeles_fizetve k ON (t.pid=k.kuid)';


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

$this->_examplecriteria = new CDbCriteria();

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

$this->_criteria->mergeWith(array(

                        'with' => array(

                            'hirdetesextra' => array(

                                'select' => false,

                                'alias' => 'e',

                                'joinType' => 'INNER JOIN',

                            ),

                        ),

                    ));

$this->_criteria->mergeWith($this->_examplecriteria);

}



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 (t.pid=k.kuid) INNER JOIN `tbl_hirdetes_extra` `e` ON (`e`.`hirdetes_id`=`t`.`id`) WHERE example = 1 ORDER BY sorrend asc, t.id 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.

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#select-detail

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




$this->_criteria->select = array(

    't.id',

    't.cim',

    ...

);

if (something) {

    $this->_criteria->select[] = 't.xxx 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.