[英]How do I join a table to itself multiple times in Propel?
我嘗試了此答案中的解決方案,但沒有成功。 結果是以下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
自身具有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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.