php - 具有与参数中传递的值一样多的结果的 SQL 原则查询

标签 php sql symfony

假设我给函数一个数组:

$arrValues = ['19', '4', '4', '18', '19']

一个函数:

$objRequeteDoctrine = $this->getEntityManager()->createQueryBuilder()
    ->select('o')
    ->from('xxxxxx', 'o')
    ->where('o.id IN (:values)')
    ->andwhere('o.actif = 1')
    ->setParameter(':values', $arrValues );

return $objRequeteDoctrine->getQuery()->getResult();

在这里,它将检索 3 个对象(删除重复项)。但是,如果我想检索 5 个重复的对象怎么办?这可能吗?

谢谢。

最佳答案

用 Doctrine 实现这一目标可能不是您问题的答案,但可以解决您的问题。 之后生成完整数组怎么样?

$arrValues = ['19', '4', '4', '18', '19'];

$objRequeteDoctrine = $this->getEntityManager()->createQueryBuilder()
    ->select('o')
    ->from('xxxxxx', 'o')
    ->where('o.id IN (:values)')
    ->andwhere('o.actif = 1')
    ->setParameter(':values', $arrValues );

$result = $objRequeteDoctrine->getQuery()->getResult();

// Get the object ids in a separate array to find their position in the result

$idsOnly = [];
foreach($result as $entity) {
    $idsOnly[] = $entity->getId();
}

// Find the key of the object by id in the first result

$fullResult = [];
foreach ($arrValues as $id) {
    $keyFound = array_search($id, $idsOnly);
    if ($keyFound !== false) {
        $fullResult[] = $result[$keyFound];
    }
}

return $fullResult;

关于php - 具有与参数中传递的值一样多的结果的 SQL 原则查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56808634/

相关文章:

php - 扩展 FOSUserBundle 以允许使用电话号码登录

php - 获取 Oracle 中存在但 MySQL 中缺失的记录的挑战

php - MySQL中如何连接两个表

php - 一起使用 COUNT 和 GROUP 时无法检索零值

php - 获取 "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version"

sql - bigquery 中最近 30 天的结果

sql - 从最大值中获取行的 ID

php - 将图像转换为字符串(用于 Symfony2 响应)

SQL Server 2005 - 同步开发/生产数据库

symfony - 在 EasyAdmin 3.x 中使用 autocomplete() 时如何设置 AssociationField 选择标签