我有一个来自 Symfony2 项目的对象。下面是代码。命名空间 Acme\UserBundle\Form\Model;
use Symfony\Component\Validator\Constraints as Assert;
use Acme\UserBundle\Entity\User;
class Registration
{
/**
* @Assert\Type(type="Acme\UserBundle\Entity\User")
*/
protected $user;
...
public function setUser(User $user)
{
//Get the EntityManager here!!!!
$group = $em
->getRepository('AcmeUserBundle:Group')
->findOneByName('Customers');
$this->user->addGroup($group);
}
....
}
那么,正如代码中强调的那样,如何让 EntityManager 使用 Doctrine 检索实体?提前致谢