【问题标题】:Symfony form many-to-many -> many-to-oneSymfony 形式多对多 -> 多对一
【发布时间】:2015-10-16 12:14:02
【问题描述】:

我有三个不同的实体 - 时事通讯 - 通讯选项 - 选项

我需要 NewsletterOptions 实体,因为我需要该实体中的额外属性,因此不能选择 manyToMany 关系。现在我想创建以下表单:

因此我创建了以下 FormTypes:

NewsletterType

<?php
class NewsletterType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('newsletterOptions', 'collection', array(
            'type' => new NewsletterOptionsType()
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Application\Bundle\Entity\Newsletter'
        ));
    }
}

NewsletterOptionsType

class NewsletterOptionsType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('option', 'entity', array(
            'property' => 'name',
            'class' => 'Bundle:Options',
            'multiple' => true,
            'expanded' => true
        ));
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Application\Bundle\Entity\NewsletterOptions'
        ));
    }

}

指定了以下实体:

Newsletter.orm.yml

Application\Bundle\Entity\Newsletter:
    type: entity
    id:
        id:
            type: smallint
            id: true
            generator:
                strategy: AUTO
            options:
              unsigned: true
    fields:
        name:
            type: string
            length: 255
    oneToMany:
        newsletterOptions:
            targetEntity: NewsletterOptions
            mappedBy: newsletter

NewsletterOptions.orm.yml

Application\Bundle\Entity\NewsletterOptions:
    type: entity
    id:
        id:
            type: smallint
            id: true
            generator:
                strategy: AUTO
            options:
                usigned: true
    manyToOne:
        newsletter:
            targetEntity: Newsletter
        option:
            targetEntity: Options
            mappedBy: newsletterOptions

Options.orm.yml

Application\Bundle\Entity\Options:
    type: entity
    id:
        id:
            type: smallint
            id: true
            generator:
                strategy: AUTO
            options:
                usigned: true
    fields:
        name:
            type: string
            length: 255
        regex:
            type: string
            length: 255
    oneToMany:
        newsletterOptions:
            targetEntity: NewsletterOptions
            mappedBy: option

当我尝试运行脚本时,会导致以下错误。设置 data_class 没有帮助。怎么解决?

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Application\Bundle\Entity\NewsletterOptions. You can avoid this error by setting the "data_class" option to "Application\Bundle\Entity\NewsletterOptions" or by adding a view transformer that transforms an instance of class Application\Bundle\Entity\NewsletterOptions to scalar, array or an instance of \ArrayAccess.

【问题讨论】:

  • Many-to-many association example in symfony 在这里完整解释。它是一对多 -> 中间表
  • 我使用与示例中相同的设置,一对多的关系是相同的。问题是如何呈现表单。
  • FormTypes 标头和 Twig templates 标头部分没有帮助吗?我想他们至少会给出一个想法。
  • 不,我无法通过。示例代码为关联表创建了一个表单。因此,在我的情况下,它只会创建一个表单,我可以在其中选择时事通讯和选项。不是我想要的形式。
  • 你可能需要entity然后输入。在此处查看LeagueType.php 并查看__construct。 inanzzz.com/index.php/post/djrc/…

标签: php symfony doctrine-orm many-to-many many-to-one


【解决方案1】:

应该是:

    $builder->add('option', 'entity', array(

(注意,option 不是 options,因为这是您的字段的调用方式)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多