我正在嘗試使用symfony2表單CollectionType與兩個實體之間的onetoMany關係構建表單。但它始終以無效參數異常結束無法加載類型「Symfony \ Component \ Form \ Extension \ Core \ Type \ CollectionType」。 我有兩個實體叫做票和附件。票有很多附件,每個附件都與單張票有關。Symfony2表單集合無法加載類型CollectionType
在門票實體:
/**
* @ORM\OneToMany(targetEntity="Attachment", mappedBy="ticket", fetch="EXTRA_LAZY", cascade={"persist"})
*/
private $attachments;
而且在連接實體:
/**
* @var integer
* @ORM\ManyToOne(targetEntity="Ticket", inversedBy="attachments", fetch="LAZY")
* @ORM\JoinColumn(name="ticket_id", referencedColumnName="id")
*/
private $ticket;
AttachmentType形式:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('file')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Attachment'
));
}
TicketType形式:
public function buildForm(FormBuilderInterface $formBuilderInterface, array $options){
$formBuilderInterface
->add("subject", "text", array(
"label"=>"RaiseTicketType.labels.subject.label",
"attr" => array(
"class" => "ui-flat",
"placeholder"=>"RaiseTicketType.labels.subject.label"
)
)
)
->add("attachments", CollectionType::class, array(
'entry_type' => AttachmentType::class,
'allow_add' => true,
'allow_delete' => true
)
);
;
}
public function setDefaultOptions(OptionsResolverInterface $optionsResolverInterface){
$optionsResolverInterface->setDefaults(array(
'data_class' => 'Ticket'
));
}
我已經導入CollectionType命名空間
需要用戶更換附件字段的完全限定類名的data_class:http://symfony.com/doc/current/book/forms.html#creating-form-classes – Cerad