简体   繁体   English

Symfony2形式带参数的query_builder

[英]Symfony2 form query_builder with parameter

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" “可捕获的致命错误:传递给Intranet \\ RhBundle \\ Form \\ AvatarFormType :: Intranet \\ RhBundle \\ Form {closure}()的参数1必须是Intranet \\ UserBundle \\ Entity \\ ImageRepository的实例,给定的Doctrine \\ ORM \\ EntityRepository的实例,在第56行的C:\\ wamp \\ www \\ projet \\ vendor \\ symfony \\ symfony \\ src \\ Symfony \\ Bridge \\ Doctrine \\ Form \\ ChoiceList \\ ORMQueryBuilderLoader.php中调用,并在C:\\ wamp \\ www \\ projet \\ src \\ Intranet中定义\\ RhBundle \\ Form \\ AvatarFormType.php第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 : 这是我的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 : 这是我的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. 用户和图像之间已经存在ManyToMany关系,用户和图像之间已经存在一个OneToOne(我注意到)。

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']. 我不确定是否必须使用use($ options)和$ options ['data']调用函数,但是使用var_dump时,我在$ options ['data']上看到了我的用户。

EDIT : 编辑:

I bring a little precision : The ImageReposiroty seems to be not found although the use is ok. 我带来一点精度:尽管可以使用,但似乎找不到ImageReposiroty。 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 : 但是,如果我放EntityReposirtoy,该错误就会消失,并且出现以下symfony错误消息:

Expected argument of type "Doctrine\\ORM\\QueryBuilder", "array" given 给定类型为“ Doctrine \\ ORM \\ QueryBuilder”,“ array”的预期参数

But I know I have to call ImageRepository and not EntityRepository... 但是我知道我必须调用ImageRepository而不是EntityRepository ...

Thank's to @R. 感谢@R。 Canser Yanbakan who gave me an example, I solve myself my problem ! 坎瑟·扬巴坎(Canser Yanbakan)给了我一个例子,我自己解决了我的问题!

In my entity I have * @ORM\\Entity But for using reposiroty I have to call it like this : 在我的实体中,我有* @ORM \\ Entity,但是要使用reposiroty,我必须这样称呼它:

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

The correct BuildForm is : 正确的Bui​​ldForm是:

 /**
 * @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 : 严格的createForm是:

$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 $ user选择用户的头像,并在query_builder中指定参数的数组

Avatar form type is expecting to data_class must be Intranet\\UserBundle\\Entity\\Image. Avatar表单类型期望data_class必须是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 比将您的setDefaults更改为

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: 比您可以得到像这样的user_id变量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM