2016-11-09 45 views
0

在我的实体主义的QueryBuilder凡在收集

/** 
* @ORM\ManyToMany(targetEntity="\UserBundle\Entity\User", mappedBy="acr_groups") 
*/ 
protected $users; 

public function __construct() { 
    $this->users = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

在我FormType我要过滤掉那些基团,其中当前用户是成员,我有用户的一个数组集合:

$builder 
    ->add('acr_group', EntityType::class, array(
     'label' => 'ATS', 
     'class' => 'HazardlogBundle:ACRGroup', 
     'query_builder' => function (EntityRepository $er) use ($user) { // 3. use the user variable in the querybilder 
       $qb = $er->createQueryBuilder('g'); 
       $qb->where(':user IN (g.users)'); 
       $qb->setParameters(array('user' => $user)); 
       $qb->orderBy('g.name', 'ASC'); 
       return $qb; 
     }, 
     'choice_label' => 'name' 
    )) 

我问题显然是在这条线:

$qb->where(':user IN (g.users)'); 

我如何使用我的用户的集合作为参数IN()?

+0

您好!你可以尝试使用'in'表达式:'$ qb-> where($ qb-> expr() - > in('g.users',$ user));' –

回答

0

在尝试了一些解决方案失败后,我最终转向了一下。我手动创建了一个我想要的ID数组。

有可能是一个原生的方式做到这一点,似乎是一个非常标准的事情......然而,这工作。

// 1. to inject user entity into this builder first make a construct function (remember to inject it from controller!) 

function __construct($user) 
{ 
    $this->user = $user; 
} 

/** 
* {@inheritdoc} 
*/ 


public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $user = $this->user; // 2. instantiate the variable we created in our construct above 

    //create group list array 
    $groupList = $this->user->getACRGroups(); 
    $gla = array(); 
    foreach ($groupList as $g) { 
     $gla[] = $g->getId(); 
    }; 

    $builder 
    ->add('acr_group', EntityType::class, array(
     'label' => 'ATS', 
     'class' => 'HazardlogBundle:ACRGroup', 
     'query_builder' => function (EntityRepository $er) use ($gla) { // 3. use the user variable in the querybilder 
       $qb = $er->createQueryBuilder('g'); 
       $qb->where('g.id IN (:gla)'); 
       $qb->setParameters(array('gla' => $gla)); 
       $qb->orderBy('g.name', 'ASC'); 
       return $qb; 
     }, 
     'choice_label' => 'name' 
    )) 
0
$q = $this->createQueryBuilder('v') 
    ->select('v') 
    ->andWhere('v.workingHours IN (:workingHours)') 
    ->setParameter('workingHours', $workingHours); 

来源:Doctrine 2 WHERE IN clause using a collection of entities

或者按照理论文档:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class 中插入条件查询生成器教义可以使用EXPR()

$qb->add('select', new Expr\Select(array('u'))) 
    ->add('from', new Expr\From('User', 'u')) 
    ->add('where', $qb->expr()->orX(
     $qb->expr()->eq('u.id', '?1'), 
     $qb->expr()->like('u.nickname', '?2') 
    )) 
    ->add('orderBy', new Expr\OrderBy('u.name', 'ASC')); 

IN的Syntaxe:

$qb->expr()->in('u.id', array(1, 2, 3)) 

另外,麦确保你不使用类似于$qb->expr()->in('value', array('stringvalue'))的东西,因为这会导致学说抛出异常。相反,使用$qb->expr()->in('value', array('?1'))并绑定你的参数?1

1

试试下面的代码

$user = array(12,211,612,84,63,23); // Assuming User Ids whose groups you want to retrive 

$builder 
->add('acr_group', EntityType::class, array(
    'label' => 'ATS', 
    'class' => 'HazardlogBundle:ACRGroup', 
    'query_builder' => function (EntityRepository $er) use ($user) { 
      $qb = $er->createQueryBuilder('g'); 
      $qb->innerJoin('g.users', 'u'); // Inner Join with users 
      $qb->where('u.id IN (:user)'); 
      $qb->setParameters(array('user' => $user)); 
      $qb->orderBy('g.name', 'ASC'); 
      return $qb; 
    }, 
    'choice_label' => 'name' 
)) 

我与doctrine2试图在symfony 2.3。您可以使用select函数与createQueryBuilder()来获取特定的列。