Answer the question
In order to leave comments, you need to log in
How to use addXXX() instead of setXXX() when generating fixtures for ManyToMany using nelmio/alice?
In my Symfony4 tutorial project, there is a ManyToMany relationship between two Entities (Image and Gallery). I'm using the nelmio/alice package to generate fixtures, which is where the difficulty comes in.
Doctrine's ManyToMany requires the addImage() and addGallery() methods to work. The entire logic of the gallery is based on these methods. But in order for the fixtures to work, the setImage() and setGallery() methods are required, which neither I nor Doctrine need. The most important thing is that these methods can be empty - fixtures still work correctly, it is important that they simply be:
// if I delete this method, the fixtures do not work - shows an error
public function setGallery(Gallery $gallery)
{
// $this->addGallery($gallery);
}
/**
* @param $gallery Gallery
*/
public function addGallery(Gallery $gallery)
{
if ($this->galleries->contains($gallery)) {
return;
}
$this->galleries->add($gallery);
$gallery->addImage($this);
}
Answer the question
In order to leave comments, you need to log in
Yes, it's just my mistake... which I'm very happy about. you don't have to write and understand property accessors, yet. Everything turned out to be trite - I stupidly compiled the yaml file of fixtures incorrectly. Where an Entity should have a plural property "gallery:", there was "gallery:". Similarly with the "images:" property.
It's amazing that it worked at all for so long, until I paid attention to the "unnecessary" methods that duplicated the functionality.
It's hard while learning.
I apologize for the completely useless thread. ))
Nikolai Egorov , Everything should work without a setter. See the manual from KNPUniversity: https://knpuniversity.com/screencast/collections/m...
Provide the text of the error. I don't think people would use this library if there was a bug you describe.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question