php Yii 只选择数组中的指定属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11566405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:46:50  来源:igfitidea点击:

Yii selecting only specified attributes in array

phpactiverecordyii

提问by Rajat Singhal

I often face this problem..

我经常遇到这个问题。。

Lets say ..In blog application, I need to email all the active users..

让我们说..在博客应用程序中,我需要给所有活跃用户发送电子邮件..

What I do is write findAll on users with some conditions of their last login being greater than some value..and get all the User objects...Then run a foreach through all the user model objects and store emails in a array, then use the array..

我所做的是在用户上次登录的某些条件大于某个值的情况下编写 findAll ......并获取所有用户对象......然后通过所有用户模型对象运行 foreach 并将电子邮件存储在数组中,然后使用阵列..

I other words what is happening in back-end is I am loading whole model, while I only need barely0.5%of that information, and then running a dirty code to get values in in array, and then process with it..

换句话说,后端发生的事情是我正在加载整个模型,而我只需要很少0.5%的信息,然后运行脏代码以获取数组中的值,然后对其进行处理。

Isn't it quite bad in performance and dirty code..

在性能和脏代码方面是不是很糟糕..

Now other approach I can think of is using commandBuilderand write query and then run same dirty code to get values in array..one problem of performance resolved..but as people say writing sql in mvc frameworks, is not a really good idea..

现在我能想到的另一种方法是使用commandBuilder和编写查询,然后运行相同的脏代码来获取数组中的值..一个性能问题解决了..但是正如人们所说在 mvc 框架中编写 sql 并不是一个好主意..

What I really want...some similar functions which give me column values in array if it is a single column, or array with column name as index if multiple columns..

我真正想要的是...一些类似的函数,如果它是单列,它们会给我数组中的列值,或者如果是多列,则以列名作为索引的数组..

So what I am thinkin of doing is implement it by extending ActiveRecord or something similar, what I want to check is if some one has already implemented something similar, or have some ideas for it..:)

所以我想做的是通过扩展 ActiveRecord 或类似的东西来实现它,我想检查的是是否有人已经实现了类似的东西,或者有一些想法..:)

采纳答案by Rajat Singhal

I created the behavior as I said, the way it works is:

我创建了我所说的行为,它的工作方式是:

$active_users_emails = User::model()->findColumn('email', 'active = 1');

**returns array('[email protected]','[email protected]')..**

Now one doesn't need to go through all activerecords and collect column in array, then proceed, and also bear weight of loading whole column..

现在不需要遍历所有activerecords并收集数组中的列,然后继续,并且还承担加载整列的重量..

You can find the extension on git ..https://github.com/rajatsinghal/CAdvancedArFindBehavior

您可以在 git 上找到扩展名 .. https://github.com/rajatsinghal/CAdvancedArFindBehavior

回答by Onkar Janwa

You must study of CDbCriteria deeply, their is all the things what your looking.

您必须深入研究 CDbCriteria,它们就是您所寻找的一切。

Have an example::

举个例子::

$criteria = new CDbCriteria;
$criteria->select = 't.first_name, t.email'; // select fields which you want in output
$criteria->condition = 't.status = 1';

$data = Users::model()->findAll($criteria);

$data is also an array output.

$data 也是一个数组输出。

回答by Zhang Buzz

In Yii2, you can use like this:

在 Yii2 中,你可以这样使用:

User::find()->select(['field1', 'field2']);

回答by hurz

This would also work without any behavior or without the addition to CActiveRecord. Nevertheless I also like the solution with the behavior.

这也可以在没有任何行为或不添加 CActiveRecord 的情况下工作。尽管如此,我也喜欢行为的解决方案。

$active_users_emails=CHtml::listData(User::model()->findAllByAttributes(array('active'=>1)), 'id', 'email');

回答by Rohit Suthar

Need to filter the model attributes using array_filterfunction. Try like this, its working great -

需要使用array_filter函数过滤模型属性。像这样尝试,效果很好 -

$criteria = new CDbCriteria;
$criteria->select = 't.first_name, t.email'; // select fields which you want in output
$criteria->condition = 't.status = 1';

$data = Users::model()->findAll($criteria);


Use array_filter before assign.


在分配之前使用 array_filter。

foreach($data as $model){
      $rows[] = array_filter($model->attributes);
   }

echo CJSON::encode($rows)


So, you will get output like this -


所以,你会得到这样的输出 -

[{"first_name" : "Rohit", "email" : "[email protected]"}, {"first_name" : "Rajat", "email" : "[email protected]"}]

回答by BKY

In same situation as question describes. After trying all above answers and some other resources here is what I am doing.

与问题描述的情况相同。在尝试了以上所有答案和其他一些资源之后,我正在做的就是这里。

$emailIds = Yii::app()->db->createCommand(
                 'SELECT email FROM users WHERE is_active = 1'
            )->queryAll();

$emailIds = array_column( $emailIds, 'email' ) );