Tom Tom - 10 months ago 38
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:


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?


Answer Source

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

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