Symfony3-如何持久化对象及其集合? [英] Symfony3 - How to persist an object with its collections?

查看:117
本文介绍了Symfony3-如何持久化对象及其集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个集合的对象人(文档,联系人,等)。
我想保存人并自动收集。
这是我的控制器:

I have an object "person" with several collections ("documents", "contacts", "etc"). I would like to save "person" and automatically the collection too. This is my controller:

    $em = $this->getDoctrine()->getManager();
    $persona = new Persona();
    $formulario = $this->createForm(
        PersonaType::class, 
       $persona,
        array('action' => $this->generateUrl('persona_create'),
              'method' => 'POST')
    );

    $formulario->handleRequest($request);

    $em->persist($persona);
    $em->flush();

当我转储 $ persona时,我拥有了收集的内容以及需要保存的所有信息,但是当我坚持下去时,我会丢失该集合中的所有信息,但不包括角色属性。

When I dump "$persona", I have the collection and all the information that I need to save, but when I persist it, I lose all the information of the collection except from the "persona" atributtes.

这是实体角色的一个集合

This is one collection of the entity "persona"

/**
 * @ORM\OneToMany(targetEntity="PersonaContacto", mappedBy="idPersona",cascade={"persist"},orphanRemoval=true)
 */
private $contactos;

 public function getContactos() {
return $this->contactos;

}

public function addContacto(PersonaContacto $persona_contacto) {
    $this->contactos->add($persona_contacto);
}

public function removeContacto(PersonaContacto $persona_contacto) {
    $this->contactos->removeElement($persona_contacto);
}

最后,这是我使用集合时表单的一部分

And finally, this is the part of the form when I use the collection

->add('contactos', CollectionType::class, array(
                // each entry in the array will be "document" field
                'entry_type' => PersonaContactoType::class,
                'prototype' => true,
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                // these options are passed to each "idioma" type
                'entry_options' => array('label' => false, 'attr' => array('class' => 'contacto-box')
                ),
            ))

这是添加和删除元素的.js文件。就像我说过的那样,这个.js被多个集合以相同的形式使用。

This is the .js that adds and removes elements. As I said, this .js is used by several collection in the same form.

jQuery(document).ready(function () {

    //var collectionCount = 0;

    jQuery('.add-another-collection').click(function (e) {
        e.preventDefault();

        var collectionList = $(this).parent().parent().parent().parent();

        // grab the prototype template
        var newWidget = collectionList.attr('data-prototype');

        // replace the "__name__" used in the id and name of the prototype
        // with a number that's unique to your emails
        // end name attribute looks like name="contact[emails][2]"
        //newWidget = newWidget.replace(/__name__/g, collectionCount);
        //collectionCount++;

        // create a new list element and add it to the list
        var newTr = jQuery('<tr></tr>').html(newWidget);
        newTr.appendTo(collectionList);

    });

    // handle the removal, just for this example
    $(document).on('click', '.remove-collection', function (e) {
        e.preventDefault();

        $(this).parent().parent().remove();

        return false;
    });
})

我不明白为什么级联仍然无法正常工作。

I don´t understand why the cascade persist is not working .

推荐答案

在拥有方中使用 mappedBy

/**
 * @ORM\OneToMany(targetEntity="PersonaContacto", mappedBy="xxx", cascade={"persist"},orphanRemoval=true)
 */

在反面使用 inversedBy

inversedBy="xxx"

更多详细信息双向关联

这篇关于Symfony3-如何持久化对象及其集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆