嵌套的CollectionTypes

时间:2016-09-14 06:12:10

标签: symfony symfony-forms

我有一个表单酒店,其中包含一些CollectionType,它还包含一些CollectionType。在这种形式中,我想添加一个父实体,它可以包含多个子节点,这些子节点可能同样包含多个实体:

class HotelType extends AbstractType{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('rooms', CollectionType::class, array(
            'entry_type' => RoomType::class,
            "label" => "Add rooms",
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
        ))

class RoomType extends AbstractType{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options){
    $builder

        ->add('pictures', CollectionType::class, array(
            'entry_type' => PictureRoomType::class,
            "label" => "Add pictures",
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
        ))
        ->add('prices', CollectionType::class, array(
            'entry_type' => PriceRoomType::class,
            "label" => "Add prices",
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
        ))
    ;

这里是实体关系:

class Room {
/**
 * @ORM\ManyToOne(targetEntity = "Hotel", inversedBy = "rooms")
 * @ORM\JoinColumn(name = "id_hotel", referencedColumnName = "id")
 */
private $hotel;

/**
 * @ORM\OneToMany(targetEntity="PictureRoom", mappedBy="room", cascade={"remove", "persist"})
 */
private $pictures;
/**
 * @ORM\OneToMany(targetEntity="PriceRoom", mappedBy="room", cascade={"remove", "persist"})
 */
private $prices;

class Hotel {
/**
 * @ORM\OneToMany(targetEntity="Room", mappedBy="hotel", cascade={"remove", "persist"})
 */
private $rooms;

酒店控制器:

class HotelController extends Controller{
/**
 * Creates a new Hotel entity.
 *
 * @Route("/new", name="hotel_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $hotel = new Hotel();
    $form = $this->createForm('AppBundle\Form\HotelType', $hotel);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        if ($hotel->getRooms() != null) {
            foreach ($hotel->getRooms() as $room) {
                if (!empty($room)) {
                    $room->setHotel($hotel);
                    if ($room->getPictures() != null) {
                        foreach ($room->getPictures() as $picture) {
                            if (!empty($picture)) {
                                $picture->setRoom($room);
                                $picture->upload();
                            }
                        }
                    }
                    if ($room->getPrices() != null) {
                        foreach ($room->getPrices() as $price) {
                            if (!empty($price)) {
                                $price->setRoom($room);
                                $price->setIdTva($price->getIdTva()->getId());
                            }
                        }
                    }
                    $room->setIdTva($room->getIdTva()->getTva());
                }
            }
        }

我有一个处理字段追加的JS文件:

$(document).ready(function () {

var fields = $('div[data-prototype]');
var childElementCount = 1;

fields.each(function (index) {
    var field = $(this);
    var elementCount = 1;

    field.parent().append('<a href="#" class="btn btn-primary" id="add-another-' + index + '"><i class="glyphicon glyphicon-plus"></i> Add an element</a>');

    var trigger = $("#add-another-" + index);
    var childCollections;

    trigger.click(function (e) {
        var newWidget = field.attr('data-prototype');
        newWidget = newWidget.replace(/label__/g, "");
        newWidget = newWidget.replace(/col-md-6/g, "col-md-12");
        newWidget = newWidget.replace(/__name__/g, elementCount);
        e.preventDefault();
        elementCount++;
        field.parent().append(newWidget);

        childCollections = field.parent().find('.collection');

        if(childCollections.length == 1){

            childCollections.parent().append('<a href="#" class="btn btn-primary" id="add-another-' + index + '-'+ elementCount + '"><i class="glyphicon glyphicon-plus"></i> Add an element</a>');
            var childTrigger = $('#add-another-' + index + '-' + elementCount);

            childTrigger.click(function (e) {
                var newChildWidget = childCollections.find('div[data-prototype]').attr('data-prototype');
                //newChildWidget = newChildWidget.replace(/__child__/g, "_" + childElementCount);
                console.log(newChildWidget);
                e.preventDefault();
                childElementCount += 1;
                console.log(childElementCount);
                childCollections.parent().append(newChildWidget);
            });

        } else if (childCollections.length > 1) {
            childCollections.each(function(childIndex){
                var childField = $(this);
                childField.parent().append('<a href="#" class="btn btn-primary" id="add-another-' + index + '-'+ elementCount + childIndex + '"><i class="glyphicon glyphicon-plus"></i> Add an element</a>');
                var childTrigger = $('#add-another-' + index + '-' + elementCount + childIndex);


                childTrigger.click(function (e) {
                    var newChildWidget = childField.find('div[data-prototype]').attr('data-prototype');
                    //newChildWidget = newChildWidget.replace(/__child__/g, "_" + childElementCount);
                    console.log(newChildWidget);

                    e.preventDefault();
                    childElementCount+= 1;
                    console.log(childElementCount);
                    childField.parent().append(newChildWidget);
                });
            })
        }
    });
});

该事件有效,当我点击&#34;添加元素&#34;时,正确的Form中附加正确的FormType。但是当我提交数据时,诸如PictureRoom或PriceRoom的孙子字段不会持久保存到数据库中。有人知道如何使用这种嵌套的CollectionType表单吗?

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

重要的是要理解,CollectionType是Symfony Form Component的一个特性,并且它不需要学说很多。

因此,使用CollectionType,您将在Form Data ArrayCollection中拥有一些对象,但是如何处理和持久化它们 - 这完全是您的责任。

就个人而言,我更喜欢使用empty_data属性来持久保存通过CollectionType创建的新实体。

有点像

   ->add('prices', CollectionType::class, array(
        'entry_type' => PriceRoomType::class,
        "label" => "Add prices",
        'allow_add' => true,
        'allow_delete' => true,
        'prototype' => true,
        'empty_data' => function (FormInterface $form) {
             $price = PriceRoom();
             $price->setRoom($form->getParent()->getData());
             return $price; 
         },
    ))

RoomPicture相同。并且您必须在任何级别cascade={"remove", "persist"}(酒店要保留的房间)。

在这种情况下,不确定$form->getParent()->getData()是否会有Room个实体,但无论如何它可以以某种方式从$form对象访问,只需用代码播放一下。

相关问题