Ordering embedded documents with Doctrine ODM

I’m trying to order my embed documents.

The field looks like this

/**
 * @ODMEmbedMany(targetDocument=Image::class, strategy="set")
 * @ODMIndex(keys={"order"="asc"})
 * @Groups({"offer:read"})
 */
protected $images = [];

The Image EmbeddedDocument

namespace AppDocumentEmbedded;

use AppDocumentTraitsNameableTrait;
use AppDocumentTraitsOrderableTrait;
use DoctrineODMMongoDBMappingAnnotations as ODM;

/**
 * @ODMEmbeddedDocument
 */
class Image
{
    use NameableTrait;
    use OrderableTrait;
    …
}

And the orderable trait

namespace AppDocumentTraits;

use DoctrineODMMongoDBMappingAnnotations as ODM;
use SymfonyComponentSerializerAnnotationGroups;

trait OrderableTrait
{
    /**
     * @ODMField(type="int")
     * @Groups({"offer:read"})
     *
     * @var int|null
     */
    private $order;

    public function getOrder(): int
    {
        return $this->order;
    }

    public function setOrder(int $order): void
    {
        $this->order = $order;
    }
}

I updated the indexes with bin/console doctrine:mongodb:schema:update

However my Images are not ordered. Are the indexes the way to do it?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *