php 使用数组的 Doctrine 查询生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11555207/
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:45:59  来源:igfitidea点击:

Doctrine Query Builder using Array

phpormdoctrinedoctrine-orm

提问by Mick

I have an array of id:

我有一个 id 数组:

Id_array; //array(2) { [0]=> int(9) [1]=> int(10) } 

I simply want to select the users using Id_array; I managed to do this when I don't have an array but just an integer:

我只想使用 Id_array 选择用户;当我没有数组而只有整数时,我设法做到了这一点:

$query = $em->createQuery('SELECT u FROM Users u WHERE u.id = ?1');
$query->setParameter(1, 9); // I tested the value 9 here
$users = $query->getResult(); 

How can I fetch all the users that correspond to Id_array?

如何获取与 Id_array 对应的所有用户?

回答by ZolaKt

Or without the query builder:

或者没有查询构建器:

$query = $em->createQuery('SELECT u FROM Users u WHERE u.id IN (:id)');
$query->setParameter('id', array(1, 9));
$users = $query->getResult();

回答by gagarine

On doctrine 2 is implemented by the repository now since 2.1 or 2.2

从 2.1 或 2.2 开始,现在由存储库实施了学说 2

findBy(array('id' => array(1, 2, 3, 4, 5));

回答by Mick

In case someone runs into something similar, I used the query builder, The key point is to use the IN statement.

如果有人遇到类似的情况,我使用了查询构建器,关键是使用 IN 语句。

//...
$qb = $this->createQueryBuilder('u');
$qb->add('where', $qb->expr()->in('u.id', ':my_array'))
->setParameter('my_array', $Id_array);