I\'m trying to create a form with data in collection type depending on the user being logged. I\'m following this chapter of the Symfony cookbook.
Everything works f
It should be easy. Do the following:
public function queryOwnedBy($user) {
$query = $this->createQueryBuilder('a')
->from('MyBundle:Article', 'a')
->innerJoin('a.owndBy', 'u')
->where('u.id = :id')
->setParameter('id', $user->getId());
return $query;
}
public function findOwnedBy($user) {
return $this->queryOwnedBy($user)
->getQuery()
->getResult();
}
Then in the form builder:
$formOptions = array(
'class' => 'Acme\DemoBundle\Entity\User',
'multiple' => false,
'expanded' => false,
'property' => 'fullName',
'query_builder' => function(EntityRepository $er) use ($user) {
return $er->queryOwnedBy($user);
},
);
EDIT
Thank's for ncatnow and unagi I've changed the previous functions to return the querybuilder
I just did a little fix of saamorim answer. The working code would be something like this:
public function queryOwnedBy($user) {
$query = $this->createQueryBuilder("u")
->where('u.id = :id')
->setParameter('id', $user->getId());
return $query;
}
public function findOwnedBy($user) {
return $this->queryOwnedBy($user)
->getQuery()
->getResult();
}