假设我有一个 Account 实体和一个 AccountData 实体(它存储一些较少使用的属性,如性别等)。
Account 和 AccountData 是一对一的关系,Account“拥有”AccountData。
我正在尝试使用 Doctrine 2/Symfony 2 找出如何根据 AccountData 中的属性提取一个帐户。
例如,如何搜索 AccountData->gender = 'female' 的所有帐户?
最佳答案
像这样使用 Doctrine 的查询生成器应该可以解决问题:
$repository = $this->getDoctrine()->getRepository('YourBundle:Account');
$query = $repository->createQueryBuilder('a')
->join('a.AccountData', 'd')
->where('d.gender = :gender')
->setParameter('gender', 'female')
->getQuery();
$female_accounts = $query->getResult();
可以查看http://symfony.com/doc/current/book/doctrine.html#joining-to-related-records举一个使用存储库类的例子。
希望对您有所帮助。
关于php - 学说 2 : How to search for an entity by its association's value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9933385/