Yii Mysql选择自定义字段

时间:2013-01-15 23:19:35

标签: php mysql yii

我希望在一个字段中有2个自定义字段。 我怎么能这样做?


代码:

$criteria = new CDbCriteria;
$criteria->select='(number*2) AS custom_field, (number*4) AS custom_field';
$criteria->order='custom_field';
$model = Events::model()->findAll($criteria);

表:

number   custom_field
------------------
2         4, 8
3         6, 12

custom_field ordered -> 4, 6, 8, 12

编辑:

<?php $this->widget('bootstrap.widgets.TbGridView',array(
    'type'=>'striped bordered condensed',
    'id'=>'recurringevents-grid',
    'dataProvider'=>$model,
    'columns'=>array(
        'custom_field',
    ),
)); ?>

输出:

Custom Field
4
6
8
12

1 个答案:

答案 0 :(得分:1)

第一。 Events.php

class Events extends CActiveRecord {
    public $custom_square, $custome_square_square;
// skip ...
    public funciton rules() {
    // skip ...
          array('custom_square, custom_square_square', 'safe'),

第二。单独的sql选择:

$criteria->select = array(
    new CDbExpression('(number*2) AS custom_square'), 
    new CDbExpression('(number*4) AS custom_square_square'),
);
$criteria->order='custom_square';
$model = Events::model()->findAll($criteria);