Sunday, April 15, 2012

How to set a related entity with Doctrine2

When you want to set a related entity to another one, no need request the related object !

Doctrine ORM provides EntityManager::getReference($entityName, $id) method that only returns a reference to a record, not the entity itself... Let's see it in action :

<php

// ...

//This line gets the reference to the Comment record with primary key is #5
$comment = $em->getReference('AlterPHPDemoBundle:Comment', 5);

$article->addComment($comment);

// ...

No comments :

Post a Comment

Comments are moderated before being published.