Symfony2表单集合:如何从集合中删除实体?

发布于 2024-12-28 05:12:34 字数 1327 浏览 0 评论 0原文

我尝试从集合中删除实体,但它不起作用。

我想我在某个地方犯了错误,但我不知道在哪里。

这是我的 updateAction 中的代码:

    $em = $this->getDoctrine()->getEntityManager();

    $entity = new Person();

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Person entity.');
    }

    $editForm   = $this->createForm(new PersonType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
        $entity = $editForm->getData();

        $em->persist($entity);
        foreach($entity->getAddresses() as $address)
        {               
            $em->persist($address);
        }
        $em->flush();                                 

        return $this->redirect($this->generateUrl('person_show', array('id' => $id)));
    }

    return $this->render('AppPersonBundle:Person:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),

    );

请注意,要删除我的实体,我从 html 中删除了 div。

我的意思是我删除了

例如。

这是正确的方法吗?

I try to remove entities from a collection but it doesn't work.

I think I have a mistake somewhere, but I don't know where.

Here the code from my updateAction:

    $em = $this->getDoctrine()->getEntityManager();

    $entity = new Person();

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Person entity.');
    }

    $editForm   = $this->createForm(new PersonType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
        $entity = $editForm->getData();

        $em->persist($entity);
        foreach($entity->getAddresses() as $address)
        {               
            $em->persist($address);
        }
        $em->flush();                                 

        return $this->redirect($this->generateUrl('person_show', array('id' => $id)));
    }

    return $this->render('AppPersonBundle:Person:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),

    );

Note that to remove my entity I remove the div from the html.

I mean I remove <div id="myapp_personbundle_persontype_address_4"> for example.

Is it the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感情废物 2025-01-04 05:12:34

感谢这个答案,我找到了更好的解决方案。您可以使用 Doctrine 的孤儿移除功能:

class Gallery
{
    //...    

    /**
     * @ORM\OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $photos;

    //...

    public function removePhotos($photo)
    {
        $this->photos->remove($photo);
        $photo->setGallery(null);
    }
}

Thanks to this answer, I found a better solution. You can use Doctrine's orphan removal feature:

class Gallery
{
    //...    

    /**
     * @ORM\OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $photos;

    //...

    public function removePhotos($photo)
    {
        $this->photos->remove($photo);
        $photo->setGallery(null);
    }
}
极致的悲 2025-01-04 05:12:34

现在,我这样做:

    [...]        
    $editForm   = $this->createForm(new PersonType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $previousCollections = array(
        'addresses' => $entity->getAddresses(),
    );        

    $request = $this->getRequest();
    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
        $entity = $editForm->getData();

        $this->deleteCollections($em, $previousCollections, $entity);

        $em->persist($entity);
        foreach($entity->getAddresses() as $address)
        {               
            $em->persist($address);
        }
        $em->flush();                                 

        return $this->redirect($this->generateUrl('person_show', array('id' => $id)));
    }
    [...]
}

private function deleteCollections($em, $init, $final)
{
    if (empty($init)) {
        return;
    }

    if (!$final->getAddresses() instanceof \Doctrine\ORM\PersistentCollection) {
        foreach ($init['addresses'] as $addr) {
            $em->remove($addr);
        }
    }
}

我希望有一天能够通过 https://github 找到解决方案。 com/symfony/symfony/issues/1540,但查找速度很慢。

For now, i do :

    [...]        
    $editForm   = $this->createForm(new PersonType(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $previousCollections = array(
        'addresses' => $entity->getAddresses(),
    );        

    $request = $this->getRequest();
    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
        $entity = $editForm->getData();

        $this->deleteCollections($em, $previousCollections, $entity);

        $em->persist($entity);
        foreach($entity->getAddresses() as $address)
        {               
            $em->persist($address);
        }
        $em->flush();                                 

        return $this->redirect($this->generateUrl('person_show', array('id' => $id)));
    }
    [...]
}

private function deleteCollections($em, $init, $final)
{
    if (empty($init)) {
        return;
    }

    if (!$final->getAddresses() instanceof \Doctrine\ORM\PersistentCollection) {
        foreach ($init['addresses'] as $addr) {
            $em->remove($addr);
        }
    }
}

And I hope a solution will be found one day with https://github.com/symfony/symfony/issues/1540, but it slow to be found.

我不会写诗 2025-01-04 05:12:34

symfony2 中的表单收集非常简单,它几乎可以为您完成所有工作。基本上,您只需要添加一个集合类型并设置allow_addallow_delete标志并添加一个小的JavaScript代码。查看食谱示例

Form collection in symfony2 is quite straightforward, it does almost all the work for you. Basically you just need add a collection type and set allow_add, allow_delete flags and add a small JavaScript code. Have a look at the cookbook example

栩栩如生 2025-01-04 05:12:34

我正在使用这个解决方案...

在控制器中:

$em = $this->getDoctrine()->getManager();
$enity->removeElements($em);

//Add all your elements again in order to update entity collection.
$entity->addElement($element) ...
....
$em->persist($entity);
$em->flush();

在实体中:

public function removeElements($em)
{
    $elements = $this->elements;

    foreach ($elements as $element) {
        $this->elements->removeElement($element);
        $em->remove($element);
        $em->persist($this);
    }

}

对我来说它有效并且显示的代码较少,而且我不必使用 orphanRemoval 功能。

I'm using this solution...

In the controler:

$em = $this->getDoctrine()->getManager();
$enity->removeElements($em);

//Add all your elements again in order to update entity collection.
$entity->addElement($element) ...
....
$em->persist($entity);
$em->flush();

In the entity:

public function removeElements($em)
{
    $elements = $this->elements;

    foreach ($elements as $element) {
        $this->elements->removeElement($element);
        $em->remove($element);
        $em->persist($this);
    }

}

For me it works and it shows less code and I don't have to use the orphanRemoval feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文