如何在Propel中多次将表连接到自身? [英] How do I join a table to itself multiple times in Propel?
本文介绍了如何在Propel中多次将表连接到自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在此答案中尝试了该解决方案 ,但无效.结果是以下SQL:
I tried the solution in this answer, but it did not work. It resulted in the following SQL:
SELECT user.id AS `Id`, user.name AS `Name`,
AS `ReferralUser.Id`, AS `ReferralUser.Name`
FROM `ReferralUser` INNER JOIN `account` ON (ReferralUser.id=account.id)
请注意,ReferralUser不是我数据库中的表,它应该是别名.
Note that ReferralUser is not a table in my database, it was meant to be the alias.
我需要至少两次将表连接到自身,但将来可能会更多.我当前的代码:
I need to join a table to itself at least two times, but possibly more in the future. My current code:
$q = \UserQuery::create();
$q->select(array('Id', 'Name',
'ReferralUserRelation.Id', 'ReferralUserRelation.Name',
'CreatorUserRelation.Id', 'CreatorUserRelation.Name'));
$q->join('ReferralUserRelation');
$q->join('CreatorUserRelation');
$q->find();
这将导致以下SQL:
SELECT user.id AS `Id`, user.name AS `Name`, user.id AS `ReferralUserRelation.Id`,
user.name AS `ReferralUserRelation.Name`, `user.id` AS `CreatorUserRelation.Id`,
`user.name` AS `CreatorUserRelation.Name` FROM `user`
INNER JOIN `user` ON (user.id=user.referral_user_id)
INNER JOIN `user` ON (user.id=user.creator_user_id)
在Propel中甚至有可能吗?
Is this even possible in Propel?
推荐答案
I recommend using table aliases and the ActiveQuery API (see http://propelorm.org/reference/model-criteria.html#table-aliases). For instance, if the User
table has a Supervisor
relationship to itself:
// Table aliases are mostly useful to join the current table,
// or to handle multiple foreign keys on the same column
$employee = EmployeeQuery::create('e')
->innerJoin('e.Supervisor s')
->where('s.Name = ?', 'John')
->find();
这篇关于如何在Propel中多次将表连接到自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文