如何在yii2使用的findAll()?

编程入门 行业动态 更新时间:2024-10-28 06:27:44
本文介绍了如何在yii2使用的findAll()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道我怎么可以得到阵列ID 用户的所有数据,其中条件

I want to know how can i get all data of user with array id for where condition

在警予,你可以做这样的事情。

In yii you could do something like this

$students = Student::model()->findAll("id IN ({$_POST['studentIds']})");

$userDtls = Student::model ()->findAllByAttributes ( array ( 'id' => explode ( ",", $_POST ['studentIds'] ) ) );

现在在 yii2 CDbCriteria 是不存在的,所以这方法我应该使用来实现同样的事情??

Now in yii2 CDbCriteria is not there, so which approach should i use to achieve same thing??

我也试过,但它仅在数组中返回第一个ID数据

I have tried this but it only returns data for first id in the array

$result = Users::findAll([ 'id'=> $_POST ['keylist']]);

在文档上记着,我们可以用这个

In documentation it is written that we can use this

$result = Users::findAll([1,488,489]);

但我的数组 $ _ POST ['键列表'] 是这样的

keylist{ 0='1' 1='5' 2='8' }

我也试过这种

$ids = \Yii::$app->request->post('keylist', []); $result = Users::findAll($ids);

和还在这里数组中的第一个ID返回数据的截图

And still returns data for first id in the array here is the screenshot

这就是为什么它不工作我猜

Thats why it doesnt work i guess

感谢您

推荐答案

$用户=用户::的findAll($ IDS); 是一个正确的做法。

请参阅您可以在 $ IDS 通过官方文档的这里。

See what you can pass in $ids in official docs here.

当我解释here,你永远不应该相信从 $ _ POST 数据,并在使用前检查它生存和验证。

As I explained you here, you should never trust data from $_POST and check it for existence and validate before using.

获取并检查它是否存在与Yii2的例子:

Example of getting and check for existence with Yii2:

$ids = \Yii::$app->request->post('ids');

或者只是:

$ids = isset($_POST['ids']) ? $_POST['ids'] : null;

对于比较复杂的情况下,我建议你创建单独的搜索模型和验证使用,请参阅GII的CRUD为例。

For more complex cases I'd recommend to create separate search model and use it with validation, see Gii's CRUD for example.

更新:注意您实际传递为 $ IDS

更多推荐

如何在yii2使用的findAll()?

本文发布于:2023-08-03 15:32:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   findAll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!