Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

How to create and persist a new entity instance inside an entity?

$
0
0

I would like to create a favorite list for each user I create on my website. It means that in each instantiation of the User entity I expect a FavoriteList to be created.

I have an entity User and an entity FavoriteList with a one to one relation.

I tried to put this idea in User::__contruct():

    public function __construct()
    {
        $list = new FavoriteList;
        $this->setFavoriteList($list);
    }

I have this message when I use the add user function of easy admin.

Entity of type "App\Entity\FavoriteList" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?

I though that using setter of a collection calls doctrine methods to persist and flush my new entity. Of course I can't call a manager directly in my User call because it's a controller method.

I precise that I don't want to instance new FavoriteList in my Register Method I want it to be created with each iteration of a user, not only when a user is created in the classic way.

What is the best way to do that?


Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>