选择活动记录中包含表达式的列

时间:2014-04-16 10:13:25

标签: php database yii

如何在yii ActiveRecord

中实现此选择查询
select idCustomer0.idCustomer ,
idCustomer0.FirstName ,
idCustomer0.LastName , 
case when idCustomer0.isDeleted = 0 then idCustomer0.CustomerName  
   else Concat(idCustomer0.CustomerName , " - Deleted") 
      end As CustomerName 
from customers idCustomer0  

我的默认范围的isDeleted列等于0

我正在尝试为已删除的客户创建一个范围,该范围将通过关系获取

我的范围代码:

public function viewDeleted()  
{  
    $alias = $this->getTableAlias(false , false);  
    $column = $this->representingColumn();  

    $allAttributes = $this->getAttributes();  

    unset($allAttributes[$column]);  

    $text = '';  
    foreach($allAttributes as $attr => $val)
    {  
        $text .= $alias . '.' . $attr . ' ,';  
    }  
    $text .= new CDbExpression( 'case when ' . $alias .'.isDeleted = 0 then '. $alias .'.'. $column .'  
                            else Concat('. $alias .'.'. $column .' , \' - Deleted\')  
                        end  As ' . $column);  

    $criteria = new CDbCriteria();  
    $criteria->select = $text;  
    $criteria->addCondition($alias.'.isDeleted = 1');  

    $this->getDbCriteria()->mergeWith( $criteria , 'or');  

    return $this;  
}

我在我的其他模型搜索方法中使用此范围

 public function search() {  
    $criteria = new CDbCriteria;  
    $criteria->with = array('idCustomer0:viewDeleted');

但结果并未显示已删除的-deleted列已删除。

如何使用自定义表达式选择我的列?

0 个答案:

没有答案