I have a BookEntity (Name,Descript, ...) with a OneToMany relations to TagEntity(name, position)

A tag is linked to only one BookEntity and a BookEntity can have multiple Tags, but can also keep the Tags sorted in an explicit order.

For that I'm using an Drag&Drop javascript plugin in order to set a input field hidden into the TagType I have embed

$builder->add('tags','collection', array(
    'type' => new TagType(),
    'allow_add' => true,
    'by_reference' => false,
    'prototype'   => true,
    'allow_delete' => true)
);

I can add new Tag using prototype.

but I have existing tags, and I'm going into the Edit page of a BookType.

How can i specify the order of the TagType ?

Thanks a lot

有帮助吗?

解决方案

referencing this issue and this one you can use doctrine's @OrderBy annotation to have the Tags ordered when fetched from database. This will affect the form-collection rendering aswell.

/**
 * @ORM\ManyToMany(targetEntity="Tag")
 * @ORM\OrderBy({"position" = "ASC"})
 */
protected $tags;

Just as a quick reminder you can order already fetched collections in your controller as described in my answer here.

其他提示

You can sort in your twig view :

{% for thing in somethings|sort('ThingIdColum') %}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top