Order of Symfony form CollectionType field

后端 未结 1 1335
滥情空心
滥情空心 2021-01-11 11:36

In my model I have a Recipe entity and Ingredient entity. In Recipe entity, the relation is defined like this:

/**
 * @ORM\\OneToMany(targetEntity=\"Ingredie         


        
相关标签:
1条回答
  • 2021-01-11 12:17

    You need to use finishView method of your form type.

    Here is the example of code:

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        usort($view['photos']->children, function (FormView $a, FormView $b) {
            /** @var Photo $objectA */
            $objectA = $a->vars['data'];
            /** @var Photo $objectB */
            $objectB = $b->vars['data'];
    
            $posA = $objectA->getSortOrder();
            $posB = $objectB->getSortOrder();
    
            if ($posA == $posB) {
                return 0;
            }
    
            return ($posA < $posB) ? -1 : 1;
        });
    }
    
    0 讨论(0)
提交回复
热议问题