Hi!
I'm trying to make a gallery where the images and categories have a relationship ManyToMany. All good, but edit when I can only from the owner side. Now even fields to edit relations with the "slave" part, no.
How can I add the ability to handle both sides? It is possible?
According to past issues reported to this repo, this is probably related to Doctrine and not this bundle. I show bellow the example I always use to set up a bidirectional many-to-many relation in Doctrine, so you can check if you have all these needed methods and configs:
// src/Entity/Article.php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity()
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", name="id")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
*/
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
public function addTag(Tag $tag)
{
if ($this->tags->contains($tag)) {
return;
}
$this->tags->add($tag);
$tag->addArticle($this);
}
public function removeTag(Tag $tag)
{
if (!$this->tags->contains($tag)) {
return;
}
$this->tags->removeElement($tag);
$tag->removeArticle($this);
}
}
// src/Entity/Tag.php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity()
*/
class Tag
{
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", name="id")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Article", mappedBy="tags")
*/
protected $articles;
public function __construct()
{
$this->articles = new ArrayCollection();
}
public function addArticle(Article $article)
{
if ($this->articles->contains($article)) {
return;
}
$this->articles->add($article);
$article->addTag($this);
}
public function removeArticle(Article $article)
{
if (!$this->articles->contains($article)) {
return;
}
$this->articles->removeElement($article);
$article->removeTag($this);
}
}
@Thanks javiereguiluz for your answer and work! Yes, it works! But had to add this line (maybe is useful to someone):
easy_admin:
entities:
Tag:
form:
fields:
- { property: 'articles', type_options: { by_reference: false } }
Thank you very much! It happens to me also and with your solution it was solved the problem.
Closing as fixed. Thanks!
Most helpful comment
@Thanks javiereguiluz for your answer and work! Yes, it works! But had to add this line (maybe is useful to someone):