I am following the doctrine documentation about sort association: Ordering To-Many Associations
I have a category entity, it has many articles:
class Category
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\Article", mappedBy="category")
* @ORM\OrderBy({"position"="ASC"})
*/
private $articles;
}
article entity has a position field for sort:
class Article
{
/**
* @Gedmo\SortablePosition
* @ORM\Column(name="position", type="integer", nullable=true)
*/
private $position;
}
and get data from controller:
/**
* @Route("/zen", name="zen")
*/
public function zen(){
$category = $this->getDoctrine()->getRepository(Category::class);
$categories = $category->createQueryBuilder('c')
->innerJoin('c.articles', 'a')
->addSelect('a')
->getQuery()->getResult();
return $this->render('index/zen.html.twig', [
'categories' => $categories
]);
}
notice above, I add inner join and addSelect for avoid N+1 query problem.
in the template:
{% for c in categories %}
{% for a in c.articles %}
position: {{a.position}}, id: {{a.id}}
{% endfor %}
{% endfor %}
the result should be ordered by position, like:
position: 1, id: 2
position: 2, id: 1
position: 3, id: 3
position: 4, id: 4
but actually ordered by id:
position: 2, id: 1
position: 1, id: 2
position: 3, id: 3
position: 4, id: 4