Order of Symfony form CollectionType field
<p>In my model I have a Recipe entity and Ingredient entity. In Recipe entity, the relation is defined like this:</p>

<pre><code>/**
 * @ORM\OneToMany(targetEntity="Ingredient", mappedBy="recipe", cascade={"remove", "persist"}, orphanRemoval=true) 
 * @ORM\OrderBy({"priority" = "ASC"})
 */
private $ingredients;
</code></pre>

<p>In Ingredient entity:</p>

<pre><code>/**
 * @ORM\ManyToOne(targetEntity="Recipe", inversedBy="ingredients")
 * @ORM\JoinColumn(name="recipe_id", referencedColumnName="id")
 */
private $recipe;
</code></pre>

<p>I am working on CRUD controller for the recipe and I want the user to be able to add ingredients dynamically. I also want the user to drag-and-drop ingredients to set their priority (order) in the recipe. I am using CollectionType form field for this.</p>

<p>and this page as tutorial:</p>

<p><a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="noreferrer">http://symfony.com/doc/current/cookbook/form/form_collections.html</a></p>

<p>Adding and showing of the recipe are working perfectly so far, however there is a problem with Edit/Update action, which I will try to describe below:</p>

<p>In the controller, I load the entity and create the form like this:</p>

<pre><code>  public function updateAction($id, Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $recipe = $em-&gt;getRepository('AppBundle:Recipe')-&gt;find($id);


      $form = $this-&gt;createEditForm($recipe);
      $form-&gt;handleRequest($request);

      ...

    }
</code></pre>

<p>Since the priority is saved in the DB, and I have <code>@ORM\OrderBy({"priority" = "ASC"})</code>, the initial loading and display of ingredients works fine. However if the user drags and drops ingredients around, the priority values change. In case there are form validation errors and the form needs to be displayed repeatedly, ingredients inside the form get displayed in the old order, even though priority values are updated.</p>

<p>For example, I have the following initial Ingredient => priority values in DB:</p>

<ul>
<li>A => 1</li>
<li>B => 2</li>
<li>C => 3</li>
</ul>

<p>The form rows are displayed in order: A,B,C;</p>

<p>After user changes the order, I have:</p>

<ul>
<li>B => 1</li>
<li>A => 2</li>
<li>C => 3</li>
</ul>

<p>but the form rows are still displayed as A,B,C;</p>

<p>I understand that the form has been initialized with order A,B,C, and updating <code>priority</code> doesn't change the element order of ArrayCollection. But I have (almost) no idea how to change it.</p>

<p>What I have tried so far:</p>

<pre><code>$form-&gt;getData();
// sort in memory
$form-&gt;setData();
</code></pre>

<p>This doesn't work, as apparently it isn't allowed to use setData() on form which already has input.</p>

<p>I have also tried to set a DataTransformer to order the rows, but the form ignores new order.</p>

<p>I have also tried to use PRE/POST submit handlers in the FormType class to order the rows, however the form still ignores the new order.</p>

<p>The last thing that (kind of) works is this:</p>

<p>In Recipe entity, define <code>sortIngredients()</code> method which sorts ArrayCollection in memory,</p>

<pre><code>  public function sortIngredients()
  {
      $sort = \Doctrine\Common\Collections\Criteria::create();
      $sort-&gt;orderBy(Array(
          'priority' =&gt; \Doctrine\Common\Collections\Criteria::ASC
      ));

      $this-&gt;ingredients = $this-&gt;ingredients-&gt;matching($sort);

      return $this;
  }
</code></pre>

<p>Then, in the controller:</p>

<pre><code>  $form = $this-&gt;createEditForm($recipe);
  $form-&gt;handleRequest($request);

  $recipe-&gt;sortIngredients();

  // repeatedly create and process form with already sorted ingredients
  $form = $this-&gt;createEditForm($recipe);
  $form-&gt;handleRequest($request);

  // ... do the rest of the controller stuff, flush(), etc
</code></pre>

<p>This works, but the form is created and processed twice, and honestly it looks like a hack... </p>

<p>I am looking for a better way to solve the problem.</p>