doupao1530 2014-05-19 13:49
浏览 106
已采纳

Symfony2使用参数形成query_builder

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条回答 默认 最新

  • douzai8285 2014-05-21 09:33
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料