![](/img/trans.png)
[英]Laravel 4 - Many-to-Many with timestamps OR Many-to-One with history
[英]Symfony form many-to-many -> many-to-one
我有三個不同的實體-新聞稿-NewsletterOptions-選項
我需要NewsletterOptions實體,因為我需要該實體中的其他屬性,所以manyToMany關系是沒有選擇的。 現在,我想創建以下表單:
因此,我創建了以下FormTypes:
<?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'
));
}
}
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'
));
}
}
指定了以下實體:
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
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
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.