How can I fetch every row where column is not in (array of values) with Doctrine?

戏子无情 提交于 2020-01-06 19:44:08

问题


I'm trying to accomplish something similar to the following SQL, but using the Doctrine API.

SELECT * FROM table_name WHERE column_name NOT IN (1, 2, 3);

I was thinking of something like this:

$entityManager->getRepository($entity)->findBy(array('ID' => array('NOT IN' => array(1, 2, 3))));

... How far am I?


回答1:


You can do this with a DQL:

$result = $entityManager->createQuery("SELECT e FROM $entity e 
        WHERE e.ID NOT IN (:ids)")
    ->setParameter('ids', array(1, 2, 3), \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
    ->getResult();

Passing arrays as parameters is supported since Doctrine version 2.1.



来源:https://stackoverflow.com/questions/13199035/how-can-i-fetch-every-row-where-column-is-not-in-array-of-values-with-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!