【问题标题】:Sorting views of a CollectionType based on a position field根据位置字段对 CollectionType 的视图进行排序
【发布时间】:2013-08-29 12:00:21
【问题描述】:

我有一个 BookEntity (Name,Descript, ...) 与 TagEntity(name, position) 具有 OneToMany 关系

一个标签只链接到一个 BookEntity,一个 BookEntity 可以有多个标签,但也可以保持标签以明确的顺序排序。

为此,我使用 Drag&Drop javascript 插件来设置隐藏在我嵌入的 TagType 中的输入字段

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

我可以使用原型添加新标签。

但我有现有的标签,我将进入 BookType 的编辑页面。

如何指定 TagType 的顺序?

非常感谢

【问题讨论】:

    标签: symfony doctrine-orm symfony-2.1


    【解决方案1】:

    引用 this issuethis one 您可以使用教义的 @OrderBy 注释在从数据库中获取标签时对标签进行排序。这也会影响表单集合的渲染。

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

    作为一个快速提醒,您可以按照my answer here 中的说明在控制器中订购已经获取的集合。

    【讨论】:

      【解决方案2】:

      您可以在树枝视图中排序:

      {% for thing in somethings|sort('ThingIdColum') %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多