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:
AdminController I can see that after posting the form the entity has an empty collection to flush, which seems correct.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?
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.
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;
}
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 ;)
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.