Yii - How to get a values array from an Active Record

前端 未结 9 2601
北荒
北荒 2021-02-15 13:12

Using Yii, how can I get an array from an Active Record.

Say something like this:

array(\'foo\', \'bar\', \'lala\')

From something like thi

9条回答
  •  遥遥无期
    2021-02-15 13:24

    Use Chtml to this is a Ugly Hack! Apply this solution is the better way to this that I found:

    public function queryAll($condition = '', $params = array())
    {
        $criteria = $this->getCommandBuilder()->createCriteria($condition, $params);
        $this->applyScopes($criteria);
        $command = $this->getCommandBuilder()->createFindCommand($this->getTableSchema(), $criteria);
        $results = $command->queryAll();
        return $results;
    }
    

    You can add this code to an ActiveRecord class, e.g.:

    class ActiveRecord extends CActiveRecord {
    //...
    }
    

    And, use this way:

    return $model->queryAll($criteria);
    

    You can read more about in this link.

提交回复
热议问题