1
我做了一些研究來回答它,但我沒有找到。使用查詢生成器或sql原則
我想知道什麼是做選擇的最佳實踐(含加盟)之間:
使用查詢生成器?
$this->getEntityManager()->createQueryBuilder()
->select('e')
->from('Module\Entity\MyEntity 'e')
->innerJoin('Module\Entity\MyEntity2', 'e2', 'WITH', 'e.e2_id = e2.id')...
->where("...")
或
使用SQL語句?
$db = $this->getEntityManager()->getConnection();
$sql = "SELECT * FROM myEntity e
INNER JOIN myEntity2 AS e2 ON e2.id = e.e2_id....
WHERE ....;"
它更安全,更快速,......?
@SBH在說DQL應該是您的默認選擇時是正確的。你的innerJoin例子不必要的複雜。 D2已經知道這些實體是如何相關的。一個簡單的innerJoin('e1.e2','e2')就是你所需要的,但大多數情況下你可能想要使用左連接。而且(在D2中)你永遠不會有一個名爲e2_id的屬性。只是e2。認爲對象,而不是ID的。 – Cerad 2014-10-29 14:10:49