2

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? Commented May 19, 2014 at 14:32
  • I have this, nothing else : /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Intranet\UserBundle\Entity\Image' )); } Commented May 19, 2014 at 14:51

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

0

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']);
})

11 Comments

I have this error with your proposition : Attempted to load class "Image" from namespace "Intranet\RhBundle\Controller\Intranet\UserBundle\Entity" in C:\wamp\www\projet\src\Intranet\RhBundle\Controller\ImageController.php line 15. Do you need to "use" it from another namespace? Perhaps you need to add a use statement for one of the following: Symfony\Component\Validator\Constraints\Image. I tried to switch user_id by user and the error is the same, so maybe I can use the entity. But to fix this bug, I don't know what's the needing use. There is already "use Intranet\UserBundle\Entity\Image;"
You are trying to load the entity from controller directory. Check the namespaces on the first lines of your controller. You can use the entity and call it shortly like Image() on your form. Send a full screenshot of your controller or paste it on a service so we can see the full codes and may help you.
Whithout "new Intranet\UserBundle\Entity\Image()" I have the same error than the first. This is a screen of my controller and my fodlers : hpics.li/7e2239f . Don't care about errors and warning on vendor and web folders, my php interpreter is too old.
Just add new Image() to line 18. Remove the full reference. You did already put the reference on line 10.
I already do it when I test your proposition when I saw the error. And there is the same error message than the first, at the same line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.