如何在表单字段上使用Symfony数据映射器,我不想直接坚持下去?

发布于 2025-02-13 12:46:05 字数 1864 浏览 0 评论 0原文

我正在构建一个名为EventDatamApper的Symfony Data Mapper类,该类别采用两个TextType表单字段,并将其转换为由学说存储的EventDetails对象。该事件的对象依次在事件对象中引用。 (DatamApper还做了其他事情。)我的mapformstodata()方法的略微简化版本如下:

公共函数mapformstodata($ forms,forms,& $ data):void {

    ...

    foreach ($forms as $element => $form) {
        switch ($form->getConfig()->getName()) {
            case 'title':
                $this->mapTitleFormToData($form, $element, $data);
                $form->getParent()->remove('title');
                break;
            case 'description':
                $this->mapDescriptionFormToData($form, $element, $data);
                $form->getParent()->remove('description');
                break;
        }
    }
    $this->mapTitleAndDescriptionFieldsToEventDetailsData($data, $forms);
}

...以及我的EventFormType的buildform()方法的略微简化版本如下:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    ...

    $builder
        ->add('title', TextType::class)
        ->add('description', TextareaType::class)
        ->setDataMapper(new EventDataMapper)
    ;
}

当我提交我的表单时,我会得到一个nosuchpropertyexception,说明以下内容:

无法确定类中属性“标题”的访问类型 “ AppBundle \ Entity \ Event”:属性“标题”也不是 方法“ addTitle()”/“ removetitle()”,“ settitle()”,“ title()”, “ __set()”或“ __call()”存在,并且可以在课堂上进行公共访问权限 “ AppBundle \ Entity \ Event” ..

如您所见,我尝试使用remove()方法将表单字段从表单中删除,以避免此例外。但是,这无济于事,这并不奇怪,因为形式的列表是按价值通过而不是通过引用传递的。 (将函数更改为通过参考结果作为方法签名作为fatalError的传递结果,然后不再与Symfony的DatamApperInterface匹配。)

由于我不会进入这里的原因,我无法使用新的表单类型来处理EventDetails,因此EventDetailsformTypepepe不是一个选择。我必须在我的EventFormType课程和EventDatamapper课程中进行此操作,并且仅与这些课程。同样,我必须将这两个TextType字段保留在当前的名称上,以免破坏团队中其他成员的工作。

是否有一种简单的方法可以在这些约束中解决此问题?

I am building a Symfony data mapper class called EventDataMapper that takes two TextType form fields and converts them into an EventDetails object stored by Doctrine. That EventDetails object is, in turn, referred to in an Event object. (The DataMapper also does some other things.) A slightly simplified version of my MapFormsToData() method looks like this:

public function mapFormsToData($forms, &$data): void
{

    ...

    foreach ($forms as $element => $form) {
        switch ($form->getConfig()->getName()) {
            case 'title':
                $this->mapTitleFormToData($form, $element, $data);
                $form->getParent()->remove('title');
                break;
            case 'description':
                $this->mapDescriptionFormToData($form, $element, $data);
                $form->getParent()->remove('description');
                break;
        }
    }
    $this->mapTitleAndDescriptionFieldsToEventDetailsData($data, $forms);
}

... and a slightly simplified version of my EventFormType's buildForm() method looks like this:

public function buildForm(FormBuilderInterface $builder, array $options): void
{
    ...

    $builder
        ->add('title', TextType::class)
        ->add('description', TextareaType::class)
        ->setDataMapper(new EventDataMapper)
    ;
}

When I submit my form, however, I get a NoSuchPropertyException stating the following:

Could not determine access type for property "title" in class
"AppBundle\Entity\Event": Neither the property "title" nor one of the
methods "addTitle()"/"removeTitle()", "setTitle()", "title()",
"__set()" or "__call()" exist and have public access in class
"AppBundle\Entity\Event"..

As you can see, I have tried using the remove() method to take the form fields out of the form in order to avoid this exception. That, however, does nothing, which is not surprising, since the list of forms is being passed by value and not by reference. (Changing the function to pass by reference results in a FatalError as the method signature then no longer matches Symfony's DataMapperInterface.)

For reasons I won't get into here, I can't use a new form type to handle EventDetails, so an EventDetailsFormType is not an option. I must do this with my EventFormType class and my EventDataMapper class, and with those classes alone. Similarly, I must keep these two TextType fields in place with the names they currently have, in order not to disrupt work by other members of my team.

Is there a simple way to fix this within these constraints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2025-02-20 12:46:06

找到了!

我只需要添加['mapped'=> false] to表单字段定义:

        ->add('title', TextType::class, ['mapped' => false])
        ->add('description', TextareaType::class, ['mapped' => false])

良好而简单。

Found it!

I just had to add ['mapped' => false] to the form field definitions:

        ->add('title', TextType::class, ['mapped' => false])
        ->add('description', TextareaType::class, ['mapped' => false])

Nice and easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文