我在Symfony2项目中遇到DQL问题。我已经定义了3个实体:
我需要获取给定的类别(slug)的子类别。我用下一个DQL得到它:
$em ->createQuery('SELECT subcat FROM SubCategory subcat WHERE subcat.id IN
(SELECT IDENTITY(csc.subCategory) FROM CategorySubCategories csc WHERE csc.category IN
(SELECT cat FROM Category cat WHERE cat.slug = :category))')
->setParameter('category', $category);
无论如何使用INNER JOINS构建此DQL?
在MysQL中例如:
SELECT subcat.slug FROM Category cat INNER JOIN CategorySubCategories csc ON(cat.id = csc.category_id)INNER JOIN SubCategory subcat ON(csc.subcategory_id = subcat.id)WHERE cat.slug喜欢“$ category”
无论如何要翻译成DQL吗?
答案 0 :(得分:0)
在我看来,没有必要有第三个实体,两个就足够了(Category和SubCategory),只需在SubCategory和Category之间添加一个关联ManyToOne,如下所述:http://docs.doctrine-project.org/en/2.1/reference/association-mapping.html