I'm using the nested set behaviour in Symfony2 with StofDoctrineExtension.
The category and post model are well configured, and the category tree works fine.
To show the posts of a category I use this query from my repository:
public function findAllPosts($category)
{
return $this->queryAllPosts($category)->getResult();
}
public function queryAllPosts($category)
{
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT p, c FROM AppBundle:Post p JOIN p.category c
WHERE c.slug = :category
ORDER BY p.created DESC
');
$query->setParameter('category', $category);
return $query;
}
But how could I do to show the posts of the children of the categories too?
I found the way. The query would be like this:
We pass an array with the children to the query, which we obtain with the function getChildren($categoryId). Remember that you have to pass the id (with this query), so you could get the ids like this: