Symfony hiddenType使用data_class作为实体而不是转换器 [英] Symfony hiddenType using data_class for entity instead of transformer

查看:92
本文介绍了Symfony hiddenType使用data_class作为实体而不是转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试将我的实体之一的ID存储在hiddenType中,然后得到:

So i tried to store the id of one of my entities in the hiddenType and i got:

该表单的视图数据应为类型标量,数组或 ArrayAccess的实例,但是AppBundle\Entity\Users类的实例。您可以通过将 data_class选项设置为 AppBundle\Entity\Users或添加一个将View类AppBundle\Entity\Users的实例转换为标量,数组或instance实例的视图转换器来避免此错误。 \ArrayAccess。

data_class :此选项用于设置表单要使用的适当数据映射器,因此您可以将其用于需要对象的任何表单字段类型。

data_class: "This option is used to set the appropriate data mapper to be used by the form, so you can use it for any form field type which requires an object."

请参阅: http://symfony.com/doc/2.7/reference/forms/types/form.html#data-class

,所以我修正了我的表格:

and so i fix my form:

$builder
    ->add('user', 'hidden', array(
        'data_class' => 'AppBundle\Entity\User',
    ));

当我尝试执行此操作时,出现一个异常,指出我的实体无法转换为字符串

when i try this i get an exception stating that my entity cannot be translated to a string

所以我在实体上实现了__tostring magic方法以返回实体的ID,然后twig能够将实体ID放入隐藏字段值中

so i implement the __tostring magic method on my entity to return the entity’s id, then twig is able to put the entity id in the hidden field value

然后当我尝试提交表单时得到:

then when i try to submit my form i get:

可捕获的致命错误:参数1传递给AppBundle\Entity\ Students :: setUser()必须是AppBundle\Entity\Users的实例,给定字符串,在第442行的/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php中调用并已定义

因此它无法将请求中的字符串值拉回到实体中以供我使用。

so its not able to pull the string value from the request back into an entity for use in my form.

是的,我已经看到了使用转换器构建一个EntityHiddenType的实现。

yes, i've seen the implementation where you build a entityHiddenType using a transformer.

但是我问是否可以使用symphony提供的data_class设置,因为我认为这是解决此问题的理想方法?

however i'm asking is this possible using the data_class setting provided by symphony as i believe this is the intended method to solve this?

我只想知道是否可以使用data_class而不是转换器来实现。以及哪种方法是最佳做法。

I just want to know if it can be achieved using data_class instead of a transformer. as well as which method is best practice.

推荐答案

我遇到了同样的问题,我通过设置<$ c $对于我的 HiddenType

I had the same issue, I solved it by setting the data_class to null in my for my HiddenType:

<?php namespace AppBundle\Forms\Signup;


use AppBundle\Entity\Course;
use AppBundle\Repository\CourseRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PreselectedCourseType extends AbstractType
{
    private $courseRepository;

    public function __construct(CourseRepository $courseRepository)
    {
        $this->courseRepository = $courseRepository;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $selectedCourse = $options['selected_course'];

        $builder
            ->add("course", HiddenType::class,['data' => $selectedCourse, 'data_class' => null]);


        $builder->get("course")->addModelTransformer(new CallbackTransformer(
            function (Course $course = null) {return $course? $course->getId():0;},
            function ($course = null) {return $this->courseRepository->getCourse($course);}
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => CourseDTO::class,
            'label' => false,
            'selected_course' => 0
        ]);
        $resolver->setRequired("selected_course");
    }
}

这篇关于Symfony hiddenType使用data_class作为实体而不是转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆