Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 276
Tom Avatar asked Oct 24 '25 19:10

Tom


1 Answers

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

<?php
/** @Entity **/
class Product
{
    /**
     * @OneToMany(targetEntity="Image")
     * @OrderBy({"seq" = "ASC"})
     **/
    private $images;
}
like image 88
Liju P M Avatar answered Oct 26 '25 10:10

Liju P M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!