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

2019-03-27 02:25发布

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

Say something like this:

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

From something like this:

MyTable::model()->findall()

9条回答
放我归山
2楼-- · 2019-03-27 02:58

if you are using Yii1.1 and you need to get ALL data from AR as array you need to care about that it self. Yii1.1 AR doesn't have this feature out of the box

In Yii2 AR has asArray() method, it's very helpful

I hope my answer helps someone

查看更多
我想做一个坏孩纸
3楼-- · 2019-03-27 03:01

For yii2 , use:

yii\helpers\ArrayHelper::map(MyModel::find()->all(), 'id', 'name'));

or

yii\helpers\ArrayHelper::getColumn(MyModel::find()->all(), 'name'));
查看更多
别忘想泡老子
4楼-- · 2019-03-27 03:01

How about:

Yii::app()->db->createCommand()
    ->setFetchMode(PDO::FETCH_COLUMN,0)
    ->select("mycolumn")
    ->from(MyModel::model()->tableSchema->name)
    ->queryAll();

The result would be:

array('foo', 'bar', 'lala')
查看更多
登录 后发表回答