jeff
jeff

Reputation: 1197

symfony2 setter for related entity id

My relation in entity:

 /**
 * @ORM\ManyToOne(targetEntity="Group")
 */
protected $group;

now I have a setter method setGroup() to set related entity, but there seems no method setGroupId() to set group_id without the entity object. How can I set group_id directly?

Upvotes: 1

Views: 1430

Answers (3)

Romain Bruckert
Romain Bruckert

Reputation: 2610

I suggest you have a look at Doctrine EntityManager ->getReference() method.

$user->setGroup($em->getReference('Group', 10));

Upvotes: 2

Jakub Zalas
Jakub Zalas

Reputation: 36241

Try defining another field (groupId) and map it directly ot your field in the database.

Upvotes: 0

Samy Dindane
Samy Dindane

Reputation: 18736

Use a custom repository to create a specific method that will fetch the group, and then set it with setGroup.

Edit: You can even directly add/update the id via a SQL query: https://stackoverflow.com/a/10215061
But that's dirty.

Upvotes: 0

Related Questions