我正在尝试使用带有notIn子句的query_builder。但这不起作用,我有此错误消息:
Call to a member function expr() on a non-object in C:\wamp\www\projet\src\Intranet\UserBundle\Entity\UserRepository.php line 301
这是方法:
public function employesSansCongesAllouesEn($annee) {
$nots = $this->createQueryBuilder('u') // récupérer les employés qui ont déjà des congés définis
->select('u.id')
->Join('\Intranet\CalendrierBundle\Entity\UserCongeByYear', 'uc', "WITH", "uc.user = u.id ") // jointure UserCongeByYear
->andWhere('u.enabled = 1 ')
->andWhere('uc.annee = :annee ') // condition d'année
->getDQL()
;
$qb = $this->createQueryBuilder('user') // récupérer les employés qui n'ont pas encore de congé pour l'année choisie
->andWhere('user.enabled = 1')
->distinct('user')
->andWhere($qb->expr()->notIn('user.id', $nots)) // condition NOT IN
->setParameter(':annee', $annee)
->orderBy('user.nom', 'ASC')
;
return $qb;
}
以及使用此查询的表单:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('users', 'entity', array(
'label' => "Employés : ",
'class' => 'IntranetUserBundle:User',
'multiple' => false,
'required' => true,
'attr' => array('class' => 'form-control'),
'query_builder' => function(UserRepository $er) use($options) {
return $er->employesSansCongesAllouesEn($options['annee']);
}
))
->add('heures', 'integer', array(
'label' => "Nombre d'heures attribuées : ",
'attr' => array('class' => 'form-control', 'min' => 1, 'max' => '250')
))
;
}
您$qb
在声明之前使用。
您应该先声明它,然后再在查询构建过程中使用它,例如。
$qb = $this->createQueryBuilder();
// récupérer les employés qui ont déjà des congés définis
$nots = $qb
->select('u.id')
->Join(
'\Intranet\CalendrierBundle\Entity\UserCongeByYear',
'uc',
"WITH",
"uc.user = u.id "
) // jointure UserCongeByYear
->andWhere('u.enabled = 1 ')
->andWhere('uc.annee = :annee ') // condition d'année
->getDQL()
;
// récupérer les employés qui n'ont pas encore de congé pour l'année choisie
return $qb
->where('user.enabled = 1')
->distinct('user')
->andWhere($qb->expr()->notIn('user.id', $nots)) // condition NOT IN
->setParameter(':annee', $annee)
->orderBy('user.nom', 'ASC')
;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句