title: Sonata/AdminBundle: Selected Option
body: <p>I'm new in Symfony and Sonata/AdminBundle. I would like to know how to mark <code>selected</code> an option when the entity has a field from other entity. For example: I have two entities: <em>Shop</em> and <em>City</em>. The <em>Shop</em> entity has a field called <code>id_city</code>. </p>
  
  <p>My problem is when I'm rendering the edit form Shop because always the first <code>id_city</code> in the option is selected.</p>
  
  <p>This is the piece of code where I'm rendering the configuration form in <code>AdminStores</code> class:</p>
  
  <pre><code>protected function configureFormFields(FormMapper $formMapper)
  {
  
      $formMapper
        -&gt;tab('Tiendas')
        -&gt;with('Content', array('class' =&gt; 'col-md-9'))
          -&gt;add('nombreTienda', 'text')
          -&gt;add('cifTienda', 'text')
          -&gt;add('direccionTienda', 'text')
          -&gt;add('personaContacto', 'text', array('required' =&gt; false,'empty_data' =&gt; ''))
          -&gt;add('cp', 'text', array('label' =&gt; 'Código Postal', 'required' =&gt; false, 'empty_data' =&gt; '00000'))
          -&gt;add('urlTienda', 'text', array('required' =&gt; false, 'empty_data' =&gt; ''))
          -&gt;add('emailTienda', 'text')
          -&gt;add('telefonoTienda', 'text')
          -&gt;add('login', 'text')
          -&gt;add('pass', 'password', array('required' =&gt; false))
          -&gt;add('idMunicipio', 'entity', array(
            'class' =&gt; 'AppBundle:Municipios',
            'choice_label' =&gt; 'municipio',
            'query_builder' =&gt; function (EntityRepository $er) {
                $lista = $er-&gt;createQueryBuilder('ss')
                    -&gt;orderBy('ss.municipio', 'ASC');
            },
            'data' =&gt; $this-&gt;subject-&gt;getIdMunicipio()
          )) // end array idMunicipio y add()
          -&gt;add('idProvincia', EntityType::class, array(
            'class' =&gt; 'AppBundle:Provincias',
            'label' =&gt; 'Provincia',
            'choice_label' =&gt; 'provincia',
            'choice_value' =&gt; 'getId',
            'by_reference' =&gt; true,
          ))
          -&gt;add('descripcionTienda', 'textarea')
        -&gt;end()
        -&gt;end()
        -&gt;tab('Multimedia')
        -&gt;with('Content', array('class' =&gt; 'col-md-3'))
          -&gt;add('fotoTienda', 'file', array(
            'label' =&gt; 'Imagenes (puedes subir hasta 6 imágenes)',
            'attr' =&gt;array('class' =&gt; 'form-control', 'multiple' =&gt; 'multiple', 'accept' =&gt; 'image/*'),
            'data_class' =&gt; null,
            'required' =&gt; false,
            'empty_data' =&gt; 'noDisponible',
          ));
  } 
  </code></pre>
  
  <p>In this piece of code, I'm recovering all cities in <code>AdminStores</code> class:</p>
  
  <pre><code>-&gt;add('idMunicipio', 'entity', array(
            'class' =&gt; 'AppBundle:Municipios',
            'choice_label' =&gt; 'municipio',
            'query_builder' =&gt; function (EntityRepository $er) {
                $lista = $er-&gt;createQueryBuilder('ss')
                    -&gt;orderBy('ss.municipio', 'ASC');
            },
            'data' =&gt; $this-&gt;subject-&gt;getIdMunicipio()
          )) // end array idMunicipio y add()
  </code></pre>
  
  <p>I would like to know, please, the logic for " if <code>this-&gt;id_city == entity-&gt;id_city</code> then, option is selected".</p>
  
  <p>Thanks in advance </p>
  
  <p>I edit this comment because I think that I solved it.</p>
  
  <p>In my <code>AdminController</code> called <code>ShopsAdmin</code> I have created a method called <code>getAllMunicipios</code> which return an array with their <code>name</code> and <code>id</code>:</p>
  
  <pre><code>$allCities = array(
    'Tokyo' =&gt; 1
    'Madrid =&gt; 2
    );
  </code></pre>
  
  <p>This is the method:</p>
  
  <pre><code>protected function getAllMunicipios()
  {
      $municipios = $this-&gt;getConfigurationPool()
        -&gt;getContainer()
        -&gt;get('doctrine')
        -&gt;getRepository('AppBundle:Municipios')
        -&gt;findBy([], ['municipio' =&gt; 'ASC']);
  
      $todosmunicipios = array();
      foreach ($municipios as $municipio) {
          $todosmunicipios[(string)$municipio-&gt;getMunicipio()] = (int)$municipio-&gt;getId();
      }
      return $todosmunicipios;
  }
  </code></pre>
  
  <p>Now my <code>AdminStores::configureFormFields</code> method like that this:</p>
  
  <pre><code>-&gt;add('idMunicipio', 'choice', array(
            'choices' =&gt; $this-&gt;getAllMunicipios(),
            'required' =&gt; false,
            'by_reference' =&gt; false,
            'data' =&gt; $this-&gt;subject-&gt;getIdMunicipio()
          ))
  </code></pre>
  
  <p>It is a good way to do it? I think that the method that return all, must be placed into the entity and not int the controller but I dont know how do it static </p>