N
N
Nikolai Egorov2018-04-16 20:44:29
symfony
Nikolai Egorov, 2018-04-16 20:44:29

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);
    }

And this situation is everywhere where there is a ManyToXXX connection - everywhere there are extra methods for setting and reading properties.
To override the behavior of the fixture generator to use add*() methods instead of set*() methods, you need to write a `Custom Accessor` ( https://github.com/nelmio/alice/blob/master/doc/ad... but I don't really I can't figure out how it works.Not at all.Hands down from desperation.I
don't think I'm in a unique situation - I often have to use ManyToXXX relationships.Please tell me how to write this service.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Egorov, 2018-04-17
@nickicool

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. ))

V
voronkovich, 2018-04-16
@voronkovich

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 question

Ask a Question

731 491 924 answers to any question