通过NOT IN Symfony2排除元素

时间:2014-08-24 14:49:40

标签: symfony doctrine-orm symfony-2.3

我正在尝试在Symfony 2.3中创建一个查询,该查询应根据另一个表中的ID排除某些元素,例如我有一个SMS表和一个SMS_Deleted表,我需要从查询中排除来自SMS_Deleted的ID。

我有第一个查询来删除短信:

$sms_exceptions = $this->getDoctrine()
    ->getEntityManager()
    ->createQueryBuilder()
    ->select(array('s.idClient'))
    ->from('AloBundle:SmsDeleted', 's')
    ->getQuery()
    ->getResult();
$sms_exceptions

我应该从SMS查询中排除所有ID。然后在SMS Repository中我有这个:

public function findByClientArray($id_client, $sms_exceptions, $from, $to, $jtStartIndex, $jtPageSize)
    {
        $qb = $this->getEntityManager()->createQueryBuilder();

        return $this->getEntityManager('voipswitch')
        ->createQueryBuilder()
        ->select(array('s.idClient, s.sentTime as date, s.toNumber as destiny, s.smsStatus as status, s.smsText as sms'))
        ->from('VoipswitchBundle:SmsOutbox', 's')
        ->where('s.idClient = :id_client AND s.sentTime >= :from AND s.sentTime <= :to')
        ->where($qb->expr()->notIn('s.idSms', $sms_exceptions))
        ->setParameter('id_client', $id_client)
        ->setParameter('from', new \DateTime($from))
        ->setParameter('to', new \DateTime($to))
        ->where($qb->expr()->notIn('s.idSms', $sms_exceptions))
        ->orderBy('s.sentTime', 'DESC')
        ->setFirstResult($jtStartIndex)
        ->setMaxResults($jtPageSize)
        ->getQuery()
        ->getArrayResult();
    }

请帮忙???

由于

1 个答案:

答案 0 :(得分:0)

我做这样的事情:

$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$query = $queryBuilder
    ->select(['f'])
    ->from('AcmeDemoBundle:FirstEntity', 'f')
    ->leftJoin('AcmeDemoBundle:SecondEntity', 's', 'WITH', 'f.sId = s.id')
    ->where($queryBuilder->expr()->isNull('s.id'))
    ...
    ...

或与协会:

$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$query = $queryBuilder
    ->select(['f'])
    ->from('AcmeDemoBundle:FirstEntity', 'f')
    ->leftJoin('f.secondEntity(ies)', 's')
    ->where($queryBuilder->expr()->isNull('s.id'))
    ...
    ...