Answer the question
In order to leave comments, you need to log in
How to write a DQL query with entity relationships?
Good afternoon.
Need help making a request. There is a product table and a category table with two levels. I am attaching the YML clippings from which the entities in Symfony are generated.
item
manyToOne:
category:
targetEntity: Category
cascade: { }
mappedBy: null
inversedBy: item
joinColumns:
category:
referencedColumnName: id
orphanRemoval: false
oneToMany:
childrenCategory:
targetEntity: Category
mappedBy: parentCategory
item:
targetEntity: Item
mappedBy: category
manyToOne:
parentCategory:
targetEntity: Category
cascade: { }
mappedBy: null
inversedBy: childrenCategory
joinColumns:
parent_category:
referencedColumnName: id
orphanRemoval: false
$query = $query->where('i.category = :category')
->setParameter('category', $categoryId);
$query = $query->join('i.category', 'c', 'WITH', 'i.id = c.parentCategory')
->where('c.parentCategory = :mainCategory')
->setParameter('mainCategory', $mainCategoryId);
SELECT DISTINCT c0_.id AS id0, c0_.id AS id1 FROM crbr_item c0_ INNER JOIN crbr_category c1_ ON c0_.category = c1_.id AND (c0_.id = c1_.parent_category) WHERE c1_.parent_category = '20
AND (c0_.id = c1_.parent_category)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question