I have the essence of the Company and the essence associated with it Album (1: m). That is, there is a company and it has a collection of photo albums. Suppose I also have a bunch of just left-handed photos and I want to make a default album out of them when outputting to the page. I create this album with my hands, add these photos to it, add an album to the collection to the company. Everything is wonderful, but this album, naturally, is at the end. I want him to be the first. Is it possible to somehow sort the?

So far, the only solution is to select all the albums from the collection into a variable, remove them from the collection, add a default album to the collection, add all the others.

    1 answer 1

    Sure you may. This is done quite simply through the comments:

    <?php /** @Entity **/ class Company { // ... /** * @OneToMany(targetEntity="Album") * @OrderBy({"sortOrder" = "ASC"}) **/ private $albums; } 

    You need to add a field for sorting, because if you sort the albums by the date you added, soon another added album to the company will become "default".

    • I add the album via new CompanyAlbum() to the already selected collection - Test
    • $company->albums->setAlbums(array_merge($myAlbum, $company->getAlbums()->toArray())) - Zhukov Roman
    • That would be cool, but since $company->albums is an ArrayCollection , doctrine:generate:entities creates only: addAlbum() removeAlbum() and getAlbums() - Test
    • And what's stopping you from adding the setAlbums method? - Zhukov Roman
    • Is it right to add something to Entity yourself? Although really, why not ... - Test