原则2:如何通过协会的价值来搜索实体? [英] Doctrine 2: How to search for an entity by its association's value?

查看:55
本文介绍了原则2:如何通过协会的价值来搜索实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Account实体和一个AccountData实体(它存储一些较少使用的属性,如性别等)。



帐户和帐户数据之间的关系是一对一的,帐户拥有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆