簡體   English   中英

Symfony形式多對多->多對一

[英]Symfony form many-to-many -> many-to-one

我有三個不同的實體-新聞稿-NewsletterOptions-選項

我需要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.

應該:

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

(注意, option不是options因為這是您的字段的調用方式)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM