php - 学说 2 : How to search for an entity by its association's value?

标签 php symfony doctrine-orm

假设我有一个 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/

相关文章:

php - 如何根据月份对 MySql 行进行分组?

image - Symfony2 LiipImagineBundle 默认图像

php - Doctrine2 - 类型时间戳 - 默认值

mongodb - 使用 MongoDB 配置 Sonata Media Bundle 时出现问题

php - Zend/Doctrine 无法执行 ORM 操作,表已经存在

php - CSV 导出脚本内存不足

java - SOAP 请求返回 wsdl 而不是预期的 SOAP 响应

javascript - 如何使用 html、css、javascript 和 php 和 mysql 作为后端为网站创建登录页面?

ajax - Symfony 如何测试 AJAX 请求

symfony - 禁用 Doctrine Timestampable 在某些更新时自动更新 `updatedAt` 字段