Mirage
Mirage

Reputation: 31568

How can i add the entity to user object in constructor in symfony2 Doctrine2

I have the user object who has manytomany relation with Group entity.

But i want that by default the user should be added to Group Group_User which has id=4 in database.

Now how can i add that in User constructor

How can use query in Entity class

Upvotes: 1

Views: 2128

Answers (1)

guillaumepotier
guillaumepotier

Reputation: 7448

You can pass what you want in your User constructor. You'll have to pass it from your controller (where your queryManger is available).

In your controller:

$group_user = $this->getDoctrine()->getEntityManager()->getRepository("Bundle:Entity")->find(4);
$user = new User($group_user);

In your construct:

public function __construct(Group $group_user)
{
    $this->$group = $group_user;
}

When you'll persist your user entity in the controller, the user and it's group relation will be directly saved.

Upvotes: 1

Related Questions