我想知道我怎么可以得到阵列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()?
发布评论