I have this error :
"Catchable Fatal Error: Argument 1 passed to Intranet\RhBundle\Form\AvatarFormType::Intranet\RhBundle\Form{closure}() must be an instance of Intranet\UserBundle\Entity\ImageRepository, instance of Doctrine\ORM\EntityRepository given, called in C:\wamp\www\projet\vendor\symfony\symfony\src\Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader.php on line 56 and defined in C:\wamp\www\projet\src\Intranet\RhBundle\Form\AvatarFormType.php line 24"
When I began to search, I found a common error on the method in the repository. But maybe it's okay...
This is my ImageRepository :
public function getImageUser(User $user)
{
$qb = $this->createQueryBuilder('i')
->where('i.user = :user ')
->setParameter('user', $user);
// Et on retourne simplement le QueryBuilder, et non la Query
return $qb;
}
This is my AvatarFormType
:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
//die(var_dump($options['data']));
$builder
->add('avatar', 'entity', array(
'class' => 'IntranetUserBundle:Image',
'property' => 'alt',
'query_builder' => function(ImageRepository $r) use($options) {
return $r->getImageUser($options['user']);}
)
);
}
The relation :
/**
* @ORM\OneToOne(targetEntity="Intranet\UserBundle\Entity\Image", cascade={"persist", "remove"})
* @Assert\Valid()
*/
private $avatar;
And this is my controller :
public function imagesDeAction(Request $request, User $user) {
$form = $this->createForm(new AvatarFormType(), $user, array('user' => $user));
$images = $this->getDoctrine()
->getRepository('IntranetUserBundle:Image')
->findByUser($user);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user->setAvatar($request->avatar);;
$em->persist($user);
$em->flush();
}
}
$avatar = $user->getAvatar();
return $this->render('IntranetRhBundle:Image:imagesDe.html.twig',array('user' => $user,'images' => $images, 'form' => $form->createView()));
}
Users have some pictures in there private directory and I want to choose an avatar. There is already a ManyToMany relation between User and Image and a OneToOne (as I noticed) between User and Image.
I'm trying to build a select list with only the pcitures of a specific user with the parameter. None of the solution I found is efficient to solve this error.
I'm not sure I have to call my function with use($options) and $options['data'] but with a var_dump I saw my User on $options['data'].
EDIT :
I bring a little precision : The ImageReposiroty seems to be not found although the use is ok. I don't have the error message "class not found". But if I put EntityReposirtoy the bug disappears and I have this symfony error message :
Expected argument of type "Doctrine\ORM\QueryBuilder", "array" given
But I know I have to call ImageRepository and not EntityRepository...