php - Yii add one more select to CDbCriteria -


i'm relatively new yii.

i fell confident raw sql still bit lost when comes orm. may dummy question.

i've retrieved necessary records building such cdbcriteria:

$criteria = new cdbcriteria(array(   'select' => 'sum(items) items',   // 'condition' => 't.items > 0 , order.storage = "'product::storage_local . '"',    'condition' => 't.items > 0 , order.storage = "' .  product::storage_local . '"',   'order' => 'sum(items) desc',   'with' => array(     'product' => array(        'select' => 'code, title, producer, local_rest',          **// 'select' => 'code, title, producer, sum(local_rest)   local_rest',**        'group' => 'product.code',     )   ),    'join' => 'inner join `order` `order` on `t`.`order_id` = `order`.`id`',    // 'group' => '`product`.`code`, `product`.`title`',    'group' => '`product`.`code`',    'together' => true )); 

i try sum of local_rest field doing group by. unfortunately not return should be.

this how tried build in cdbcriteria:

// 'select' => 'code, title, producer, sum(local_rest) local_rest',. - no luck.

i can using separated query as:

$sum_local_rest = yii::app()->db->createcommand("   select  id,code, title, sum(local_rest) sum_rest product group code   order `sum_rest`  desc "); 

one more caution - there duplicate records in product table. i.e. have same product more 1 time. if use group by helps delimiter shortcoming. due bad db design , should fixed in future.

the problem need somehow bind cdbcriteria, because it's used cdataprovider , cdataprovider used gridview.

any tips how connect these 2 question in 1 cdbcriteria?

thanks in advance

edit

looking @ current answers feel need summarize. main problem need tell cdbcriteria retrieve records (bound has_many connections) , calculate sum of these records , make cdbcriteria group of these records. no other way. can't explicitly. because pass cdbcriteria cdataprovider , should run queries. how things work in yii (as far understand).

you don't have pass elements criteria. try split criteria more code this:

 $criteria = new cdbcriteria();  $criteria->select = 'sum(items) items, ' . product::storage_local;  $criteria->condition = 't.items > 0 , order.storage = ' .  product::storage_local;  //etc. 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -