我有一個時間轉換以下原料MySQL查詢,它已經過測試,效果非常好,成學說1.2赫克:生的MySQL翻譯成學說1.2
SELECT
r.name AS regionname,
s.name AS statename
FROM
job j
LEFT JOIN
community c ON c.id = j.community_id
LEFT JOIN
state s ON s.id = c.state_id
LEFT JOIN
region r ON r.id = s.region_id
WHERE
r.id = 1
這不起作用:
$q = Doctrine_Query::create()
->select('r.name AS regionname', 's.name AS statename')
->from('job j')
->leftJoin('community c ON c.id = j.community_id')
->leftJoin('state s ON s.id = c.state_id')
->leftJoin('region r ON r.id = s.region_id')
->where('r.id = 1')
->execute();
這裏是我的數據庫結構,如果它是有用的:
job:
columns:
id
community_id
relations:
community: local_key: community_id, foreign_key: id, foreignAlias: Communitys
community:
columns:
id
state_id
relations:
state: local_key: state_id, foreign_key: id, foreignAlias: States
state:
columns:
id
name
region_id
relations:
region: local_key: region_id, foreign_key: id, foreignAlias: Regions
region:
columns:
id
name
感謝詳細的解答。不幸的是,它不工作。我也試圖關閉外國別名,但它被掛在那些上面。這是我在嘗試使用它們時遇到的錯誤:「未知關係別名社區」每當我嘗試使用它們時,都會收到這些錯誤。我會檢查他們在安裝方法中的內容並重新發布。再次感謝。 – Patrick 2011-12-24 16:38:59
......並且看起來確實是這個問題。沒有社區作爲社區的聲明,只是'社區,陣列(')。這讓我困惑了一下,因爲我的模式確實將它聲明爲外部別名。但是我的模式並沒有將它識別爲一對一的關係。這是問題嗎? – Patrick 2011-12-24 16:47:22
更新:我更改了基類以包含「as」子句(我知道這是不好的形式,如果它最終解決了問題,我會在模式中更改它),並且確實解決了「社區」關係的問題然而,現在它被掛在「國家」條款上,給我一個錯誤,說該工作必須與國家相關......它不在我的模式中......社區與國家相關。任何想法的解決方案? – Patrick 2011-12-24 17:01:01