原则2:如何通过协会的价值来搜索实体? [英] Doctrine 2: How to search for an entity by its association's value?
问题描述
帐户和帐户数据之间的关系是一对一的,帐户拥有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 for a例如使用存储库类。
希望有帮助。
Let's say I have an Account entity, and an AccountData entity (which stores some less used properties, like gender, etc).
The relationship between Account and AccountData is one-to-one, and the Account "owns" AccountData.
I'm trying to figure out, using Doctrine 2 / Symfony 2, how to pull up an Account according to a property in AccountData.
For example, how do I search for all Accounts with AccountData->gender = 'female'?
Using Doctrine's Query Builder like this should do the trick:
$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();
You can check http://symfony.com/doc/current/book/doctrine.html#joining-to-related-records for an example using a repository class instead.
Hope it helps.
这篇关于原则2:如何通过协会的价值来搜索实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!