[英]Doctrine query: best approach to creating a select query joining three tables?
I have three tables that I need to join to extract a result set, and I am not sure if I should use createQueryBuilder, createNativeQuery, or some other approach. 我需要加入三个表来提取结果集,但是我不确定是否应该使用createQueryBuilder,createNativeQuery或其他方法。
My three tables are 我的三张桌子是
The Entities are properly annotated in the code. 在代码中正确注释了实体。 For example In the Email Entity:
例如,在电子邮件实体中:
/**
* @ORM\ManyToOne(targetEntity="Member")
* @ORM\JoinColumn(name="member", referencedColumnName="id", nullable=true)
*/
protected $member;
In the Member Entity: 在成员实体中:
/**
* @ORM\ManyToOne(targetEntity="Company")
* @ORM\JoinColumn(name="current_company", referencedColumnName="id", nullable=true)
*/
protected $current_company;
And in the Company Entity: 在公司实体中:
/**
* @ORM\ManyToMany(targetEntity="Member", mappedBy="companies")
*/
protected $members;
What I need to do is extract a set of records from Email where the Members are all associated with one Company. 我需要做的是从电子邮件中提取一组记录,其中成员都与一个公司相关联。
The following SQL in MySQL returns what I need, but I am new to Doctrine and do not know how best to translate this query into something Doctrine can use to extract the same results: MySQL中的以下SQL返回了我所需的内容,但是我是Doctrine的新手,并且不知道如何最好地将此查询转换为Doctrine可以用来提取相同结果的内容:
select e.* from email e
join member m on m.id = e.member
join company c on c.id = m.current_company
where m.current_company = '95f1b5a4-03c9-11e9-85b1-989096db2d5f';
Can anyone help, and which approach should be employed createQueryBuilder, createNativeQuery, etc? 任何人都可以提供帮助,应采用哪种方法createQueryBuilder,createNativeQuery等?
Update, I was able to get it working using plain old SQL, but I am not sure if this is an acceptable workaround in the Doctrine world. 更新,我能够使用普通的旧SQL使其正常工作,但是我不确定这在Doctrine世界中是否可以接受。 Thoughts and suggestions from those with more experience would be greatly welcome!
欢迎有经验的人士提出意见和建议!
$conn = $this->em->getConnection();
$sql = "select e.* from fitchek.email e join fitchek.member m on m.id = e.member join fitchek.company c on c.id = m.current_company where m.current_company = ?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, $company_uuid);
$stmt->execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.