Tom Tom - 2 months ago 6
PHP Question

Add sort to child one-to-many filter in Symfony

I have made an relation between an Product en its child images. An normal one-to-many association.

With the following code i get the child images in an object:

$product->getImages();


The I want to walk through the images to modify something, I do this with:

foreach ($product->getImages() as $Image) {
// do something
}


Only the images have an field called seq what is an number that represents the order of the images. How can I order the object related to the seq number?

Thanks!

Answer

You have to use annotation @orderBy for this. Please use something like

<?php
/** @Entity **/
class Product
{
    /**
     * @OneToMany(targetEntity="Image")
     * @OrderBy({"seq" = "ASC"})
     **/
    private $images;
}