1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But how will i get that group with id=4 in the constructor. youmean to say that i have to do in controller. i was looking if i can do that in Entity itself
It's a bad idea IMO to manipulate EntityMangager in an Entity. An Entity should be agnostic of the EM and just work with others entities. I use this in my project, an object is related to a pricing plan, I pass the pricing plan object through the constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.