tôi có 3 loại hình thức trong symfony2Làm thế nào để tự động thêm của các bộ sưu tập trong các bộ sưu tập trong các loại hình thức Symfony2
FaultType đó là mẹ của tất cả các bộ sưu tập tiếp theo
<?php
namespace My\FaultBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class FaultType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title')
->add('steps', 'collection', array(
'type' => new StepType(),
'allow_add' => true,
'prototype' => true,
'by_reference' => false,
))
->add('created')
->add('updated')
;
}
public function getDefaultOptions()
{
return array(
'data_class' => 'My\FaultBundle\Entity\Fault'
);
}
public function getName()
{
return 'my_fault_fault';
}
}
StepType:
<?php
namespace My\FaultBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class StepType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('body')
->add('photos', 'collection', array(
'type' => new PhotoType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
))
;
}
public function getDefaultOptions()
{
return array(
'data_class' => 'My\FaultBundle\Entity\Step'
);
}
public function getName()
{
return 'my_fault_step';
}
}
và Loại ảnh cuối cùng:
<?php
namespace My\FaultBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PhotoType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('description')
->add('filename')
;
}
public function getDefaultOptions()
{
return array(
'data_class' => 'My\FaultBundle\Entity\Photo'
);
}
public function getName()
{
return 'my_fault_photo';
}
}
Tôi tìm thấy bài viết xuất sắc link về nguyên mẫu, và với một loại hình lồng nhau là rất tốt, nhưng tôi có một vấn đề khi muốn có được điều này để làm việc với tổ thứ ba có nghĩa là PhotoType ... Hình ảnh trong bộ sưu tập của các bước, đó là bộ sưu tập Lỗi ..., làm cách nào để tôi có thể tự động thêm/xóa ảnh cho các bước bằng ví dụ này ...?
Hãy cân nhắc thêm nội dung liên quan bạn đã liên kết vào câu trả lời của mình. Xem: [** Cung cấp ngữ cảnh cho các liên kết **] (http://stackoverflow.com/help/how-to-answer) –