从findAll函数Yii自定义get列

I have some problem that, I am using criteria to customize a number column query

 $criteria=new CDbCriteria();
            $criteria->select =array('CompanyName', 'CompanyCountCoupon','CompanyDes', 'CompanyLogo');
            $models = Company::model()->findAll($criteria);

After I put it to array and echo result

    $rows = array();
        foreach($models as $i=>$model1) {
            $rows[$i] = $model1->attributes;
        }
    echo  CJSON::encode($rows)

My problem is that the results contains all attributes of table, and attributes not in criteria->select will set = null

   {"CompanyName":"abc","CompanyCountCoupon":"0","CompanyDes":"Hello","CompanyLogo":"\/upload\/company\/abc.jpg",**"CompanyID":null,"CompanyWebSite":null,"CompanyAdrress1":null,"CompanyAdrress2":null,"CompanyPhone1":null,"CompanyPhone2":null**}

Please help me. Thanks to all

if you go with findAll() (using ActiveRecord) you won't be able to control that part, the way to go is a custom query :

$results = Yii::app()->db->createCommand()
    ->select('CompanyName ,CompanyCountCoupon ,CompanyDes ,CompanyLogo')
    ->from('company')
    //->where() // where part
    ->queryAll();

echo  CJSON::encode($results);

now its already good to be JSON encoded and also much faster than regular ActiveRecord

Use getAttributes()

Example

 $rows = Company::model()->getAttributes(array('CompanyName','CompanyCountCoupon','CompanyDes', 'CompanyLogo')); 
 echo  CJSON::encode($rows);

This is correct behaviour.

You are asking for specific columns, so this is being correctly provided.

Recall that the attributes is part of the model, not the query.

$model = Company::model()->findByPK();
print_r($model);
 ...
/* Company points to the TABLE. not the query */
class Company extends CActiveRecord
{
---
}