1
votes

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...

2
Ok basic problem is the form expecting the ImageRepository entity from you, but you are giving EntityRepository to it. Do you have any any data_class option?Canser Yanbakan
I have this, nothing else : /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Intranet\UserBundle\Entity\Image' )); }Deynis

2 Answers

7
votes

Thank's to @R. Canser Yanbakan who gave me an example, I solve myself my problem !

In my entity I have * @ORM\Entity But for using reposiroty I have to call it like this :

* @ORM\Entity(repositoryClass="Intranet\UserBundle\Entity\ImageRepository")

The correct BuildForm is :

 /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
       ->add('avatar', 'entity', array(
            'class' => 'IntranetUserBundle:Image',
            'property' => 'alt' ,
            'query_builder' => function(ImageRepository  $r) use($options) {
                                return $r->getImagesUser($options['user']);}
            )
        );
}

And the rigth createForm is :

$form = $this->createForm(new AvatarFormType(), $user, array('user' => $user));

$user to select the user's avatar, and the array for the parameter given at the query_builder

0
votes

Avatar form type is expecting to data_class must be Intranet\UserBundle\Entity\Image. But you are giving the User entity to the form. You have to give that user variable as an option.

$form = $this->createForm(new AvatarFormType(), new Image(), array('user_id' => $user->getId());

Than change your setDefaults to

public function setDefaultOptions(OptionsResolverInterface $resolver)
{ 
    $resolver->setDefaults(array('data_class' => 'Intranet\UserBundle\Entity\Image', 'user_id = false ));
}

Than you can get user_id variable like this:

'query_builder' => function(ImageRepository  $r) use($options) {
    return $r->getImageUser($options['user_id']);
})