Easyadminbundle: OneToMany: "Remove item" not working

Created on 6 Jun 2017  路  6Comments  路  Source: EasyCorp/EasyAdminBundle

Hi. I'm working with Easyadmin 1.16.9, Symfony 3.2.8 and latest Doctrine.

I have an entity "Article", which has a OneToMany relationship to the entity "Url". I'm using a collection to manage the entities associated with Article. I can add and edit items in the collection, so far no problem.

When I try to remove an item via the UI, however, the record is not removed from the database.

So here is what I found out:

  1. When debugging AdminController I can see that after posting the form the entity has an empty collection to flush, which seems correct.
  2. I think the issue could have to do with the fact that the association is mapped by the Url entity and not by Article or a join table. (similar as described in this post).

My relationships looks like this...

Article:

    /**
     * @ORM\OneToMany(targetEntity="Url", mappedBy="article", cascade={"persist", "remove"})
     */
    private $urls;

Url:

    /**
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="urls")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="article_id", referencedColumnName="id")
     * })
     */
    private $article;

Is this something that can be or should be taken care of by EasyAdmin or do I have to do a custom implementation - for example by overwriting preUpdateEntity to force the entitymanager to delete the Url object(s) prior to updating the entity?

Most helpful comment

Hey @phpPhil,

Did u implement addUrl(), getUrls() and removeUrl() methods in your Article Entity?
Did u initialize a new ArrayCollection() on your collection in your constructor?

And you can add the annotation orphanRemoval=true to your OneToMany.

All 6 comments

Hey @phpPhil,

Did u implement addUrl(), getUrls() and removeUrl() methods in your Article Entity?
Did u initialize a new ArrayCollection() on your collection in your constructor?

And you can add the annotation orphanRemoval=true to your OneToMany.

Hi @laurent-bientz,

Just had some time to revisit this broken code.

  1. Yes, I do have those methods on the article entity and I do initialise the ArrayCollections:
    public function __construct()
    {
        $this->urls = new ArrayCollection();
       [...]
    }

   [...]

    /**
     * Add url
     *
     * @param Url $url
     *
     * @return Article
     */
    public function addUrl(Url $url)
    {
        $url->setArticle($this);

        $this->urls[] = $url;

        return $this;
    }

    /**
     * Remove url
     *
     * @param Url $url
     */
    public function removeUrl(Url $url)
    {
        $this->urls->removeElement($url);
    }

    /**
     * Get url
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getUrls()
    {
        return $this->urls;
    }
  1. I tried orphanRemoval=true, but it does not solve the problem. Reading on the Doctrine documentation, it explains that it would only delete the Url record if I would delete the article. This is not the behaviour I'm looking for. I want to remove a URL on the Article form without removing the entire article entity.

Any other ideas?

In the config of article check by_reference option is set to false.

@phpPhil: any news?

If I omit orphanRemoval=true, I got the same result than u, nothing is deleted, with the annotation it's working like a charm!

I know the documentation is confusing but did u try?

a working example, i've recheck:

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\HomeDish", mappedBy="home", cascade={"persist"}, orphanRemoval=true)
 * @Assert\Valid()
 * @Assert\Count(min="1", minMessage="Vous devez ajouter au moins 1 plat 脿 la carte.")
 */
private $homeDishes;

edit: see this thread for orphanRemoval explanations.
edit2: according to him #1689, it solves the pb ;)

@blackatze93 : Yes, it is set to false.
@laurent-bientz : Thanks, I just tried this and it solves the problem. I did try this previously but with a different result, at least that's what I remember.

However, it works now and I can't reproduce a failure - so I'm blaming myself, I must have done something wrong :D

Thanks again for your help!

Alright, so you can close ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shakaran picture shakaran  路  4Comments

haithem-rihane picture haithem-rihane  路  4Comments

ziobudda picture ziobudda  路  4Comments

seb-jean picture seb-jean  路  3Comments

Wait4Code picture Wait4Code  路  3Comments