简体   繁体   English

Symfony2表单删除实体数据

[英]Symfony2 Form Dropping Entity Data

This questions is NOT about how to embed an entity that is simple and straightforward my question is about an abnormality I am seeing and can't reconcile. 这个问题不是关于如何嵌入简单明了的实体的,我的问题是关于我所看到的并且无法调和的异常。

I have an Entity (Contact) I have an "Email" relation on this and want to have the ability to embed Emails in my create new method. 我有一个实体(联系人),对此具有“电子邮件”关系,并且希望能够在我的创建新方法中嵌入电子邮件。

So I do the usual embed steps: 1) Add my collection to my form (contactType.php) 因此,我执行通常的嵌入步骤:1)将我的收藏集添加到表单(contactType.php)

->add('emails', 'collection', array(
       'type' => new EmailType()
      ))

2) In the Controller I add a new empty Email entity and set that as my default data for my form 2)在Controller中,我添加一个新的空Email实体,并将其设置为表单的默认数据

$entity = new Contact();
$email = new Email();
$entity->addEmail($email);
$form->createForm(new ContactType(), $entity);

This is where the issue occurs, going into "createForm" my $entity has a collection called Emails, coming out the $form is a blank ContactType, no Email attached to it... it's as if the defaultData is just being ignored. 这是发生问题的地方,进入“ createForm”,我的$ entity有一个名为Emails的集合,出来的$ form是一个空白的ContactType,没有附加任何Email ...就好像defaultData被忽略了一样。

If you have seen this or have any idea why this would happen I would love some input. 如果您已经看到此事或有任何想法为什么会发生这种情况,我希望您能提供一些意见。

We have modified our getter to return an Associative Array and this seems to break the Form builder, The following breaks the Form Builder: 我们已经修改了getter以返回一个关联数组,这似乎破坏了Form构建器,以下破坏了Form Builder:

 public function getContacts()
{
    $array = new ArrayCollection();
    $property = (is_array($this->contacts)) ? $this->contacts : $array;

    foreach($property as $entity)
    {
        $array[$entity->getType()->getName()] = $entity;
    }

    return $array;
}

the basic getter is just 基本的吸气剂就是

return $this->contacts; 

Which works, it seems ArrayCollection() isn't a simple Associative Array as we assumed it could be. 哪个可行,看来ArrayCollection()并不是我们想象的那样简单的关联数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM