我在this answer中尝试了该解决方案,但没有成功。结果是以下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不是我数据库中的表,它是别名。
我需要将一个表至少自身连接两次,但将来可能会更多。我当前的代码:
$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中甚至有可能吗?
最佳答案
我建议使用表别名和ActiveQuery API(请参见http://propelorm.org/reference/model-criteria.html#table-aliases)。例如,如果User
表与其自身具有Supervisor
关系:
// 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();