I'm using the FormType for an Entity of mine, and setting up an entity field. I need two Where clauses in an And, and from what I've read on the Query Builder page, this is at least how I should go about it:
我正在使用FormType作为我的实体,并设置实体字段。我在And中需要两个Where子句,而且从我在Query Builder页面上看到的内容来看,这至少是我应该如何去做的:
'query_builder' => function ($er){
$qb = $er->createQueryBuilder('p');
$qb
->where($qb->expr()->andx(
$qb->expr()->in('p', '?1'),
$qb->expr()->not(
$qb->expr()->eq('p.location', 'NULL')
)
))
->setParameter(1, $this->totalScope)
;
return $qb;
},
However, the not(eq('col', 'NULL'))
doesn't achieve the desired result(and in fact, errors with "Error: Expected Literal, got 'NULL'"
但是,not(eq('col','NULL'))没有达到预期的结果(事实上,错误“错误:预期文字,得到'NULL'”
2 个解决方案
#1
42
You can use isNotNull
:
你可以使用isNotNull:
'query_builder' => function ($er){
$qb = $er->createQueryBuilder('p');
$qb
->where($qb->expr()->andx(
$qb->expr()->in('p', '?1'),
$qb->expr()->isNotNull('p.location')
))
->setParameter(1, $this->totalScope);
return $qb;
},
#2
28
You can also use DQL in your queryBuilder, which is much less ugly IMO.
你也可以在queryBuilder中使用DQL,这不是那么难看的IMO。
Quick and dirty example from a controller:
来自控制器的快速而肮脏的示例:
$repo = $this->getDoctrine()->getRepository('AcmeBundle:Transaction');
$query = $repo->createQueryBuilder('t')
->where('t.timestamp > :timestamp')
->andWhere('t.pinNumber IS NOT NULL')
->setParameter('timestamp', new \DateTime('1 day ago'))
->getQuery()
;
Easier to read in my estimation.
我估计更容易阅读。
#1
42
You can use isNotNull
:
你可以使用isNotNull:
'query_builder' => function ($er){
$qb = $er->createQueryBuilder('p');
$qb
->where($qb->expr()->andx(
$qb->expr()->in('p', '?1'),
$qb->expr()->isNotNull('p.location')
))
->setParameter(1, $this->totalScope);
return $qb;
},
#2
28
You can also use DQL in your queryBuilder, which is much less ugly IMO.
你也可以在queryBuilder中使用DQL,这不是那么难看的IMO。
Quick and dirty example from a controller:
来自控制器的快速而肮脏的示例:
$repo = $this->getDoctrine()->getRepository('AcmeBundle:Transaction');
$query = $repo->createQueryBuilder('t')
->where('t.timestamp > :timestamp')
->andWhere('t.pinNumber IS NOT NULL')
->setParameter('timestamp', new \DateTime('1 day ago'))
->getQuery()
;
Easier to read in my estimation.
我估计更容易阅读。