add parent category in category, fix pager

This commit is contained in:
Simon Vieille 2022-02-19 12:13:30 +01:00
commit b839f4df0b
8 changed files with 114 additions and 11 deletions

View file

@ -18,15 +18,29 @@ class PostRepositoryQuery extends RepositoryQuery
parent::__construct($repository, 'p', $paginator);
}
public function inCategory(Category $category)
public function inCategory(Category $category, bool $strict = true)
{
$c = 'c'.mt_rand();
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id = :category')
->setParameter(':category', $category->getId())
;
if ($strict) {
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id = :category')
->setParameter(':category', $category->getId())
;
} else {
$ids = [$category->getId()];
foreach ($category->getCategories() as $childCategory) {
$ids[] = $childCategory->getId();
}
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id IN (:categories)')
->setParameter(':categories', $ids)
;
}
return $this;
}