Ich bin neu in Symfony und Sonata/AdminBundle. Ich würde gerne wissen, wie man selected
eine Option markiert, wenn die Entität ein Feld von einer anderen Entität hat. Zum Beispiel: Ich habe zwei Entitäten: Shop und Stadt. Die Shop Einheit hat ein Feld namens id_city
.Sonata/AdminBundle: Ausgewählte Option
Mein Problem ist, wenn ich das Bearbeitungsformular Shop rendere, weil immer die erste id_city
in der Option ausgewählt ist.
Dies ist das Stück Code, wo ich die Konfigurationsform in AdminStores
Klasse bin Rendering:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Tiendas')
->with('Content', array('class' => 'col-md-9'))
->add('nombreTienda', 'text')
->add('cifTienda', 'text')
->add('direccionTienda', 'text')
->add('personaContacto', 'text', array('required' => false,'empty_data' => ''))
->add('cp', 'text', array('label' => 'Código Postal', 'required' => false, 'empty_data' => '00000'))
->add('urlTienda', 'text', array('required' => false, 'empty_data' => ''))
->add('emailTienda', 'text')
->add('telefonoTienda', 'text')
->add('login', 'text')
->add('pass', 'password', array('required' => false))
->add('idMunicipio', 'entity', array(
'class' => 'AppBundle:Municipios',
'choice_label' => 'municipio',
'query_builder' => function (EntityRepository $er) {
$lista = $er->createQueryBuilder('ss')
->orderBy('ss.municipio', 'ASC');
},
'data' => $this->subject->getIdMunicipio()
)) // end array idMunicipio y add()
->add('idProvincia', EntityType::class, array(
'class' => 'AppBundle:Provincias',
'label' => 'Provincia',
'choice_label' => 'provincia',
'choice_value' => 'getId',
'by_reference' => true,
))
->add('descripcionTienda', 'textarea')
->end()
->end()
->tab('Multimedia')
->with('Content', array('class' => 'col-md-3'))
->add('fotoTienda', 'file', array(
'label' => 'Imagenes (puedes subir hasta 6 imágenes)',
'attr' =>array('class' => 'form-control', 'multiple' => 'multiple', 'accept' => 'image/*'),
'data_class' => null,
'required' => false,
'empty_data' => 'noDisponible',
));
}
In diesem Stück Code, ich bin alle Städte in AdminStores
Klasse erholt:
->add('idMunicipio', 'entity', array(
'class' => 'AppBundle:Municipios',
'choice_label' => 'municipio',
'query_builder' => function (EntityRepository $er) {
$lista = $er->createQueryBuilder('ss')
->orderBy('ss.municipio', 'ASC');
},
'data' => $this->subject->getIdMunicipio()
)) // end array idMunicipio y add()
Ich würde gerne wissen, bitte, die Logik für "Wenn this->id_city == entity->id_city
dann, Option ausgewählt ist".
Vielen Dank im Voraus
ich diesen Kommentar bearbeiten, weil ich denke, dass ich es gelöst.
In meinem AdminController
ShopsAdmin
genannt habe ich eine Methode erstellt getAllMunicipios
genannt, die ein Feld mit ihren name
und id
zurück:
$allCities = array(
'Tokyo' => 1
'Madrid => 2
);
Dies ist die Methode:
protected function getAllMunicipios()
{
$municipios = $this->getConfigurationPool()
->getContainer()
->get('doctrine')
->getRepository('AppBundle:Municipios')
->findBy([], ['municipio' => 'ASC']);
$todosmunicipios = array();
foreach ($municipios as $municipio) {
$todosmunicipios[(string)$municipio->getMunicipio()] = (int)$municipio->getId();
}
return $todosmunicipios;
}
Nun meine AdminStores::configureFormFields
Verfahren wie dass dies:
->add('idMunicipio', 'choice', array(
'choices' => $this->getAllMunicipios(),
'required' => false,
'by_reference' => false,
'data' => $this->subject->getIdMunicipio()
))
Es ist ein guter Weg, es zu tun? Ich denke, dass die Methode, die alle zurückgibt, muss in die Entität und nicht Int der Controller platziert werden, aber ich weiß nicht, wie es statische
Vielen Dank für Ihre Hilfe. Ich denke, dass ich etwas mache, das nicht stimmt. Ich habe die Methode erstellt, die Sie mir vorschlagen und vor dem Rendern des Formulars gab ich ihm den Parameter City, aber nicht funktioniert ... Wenn ich das Feld in TextField ändern, wird die ID-Stadt korrekt wiederhergestellt, aber wenn ich die Eingabe ändern In ein Auswahlfeld werden alle Städte angezeigt, aber immer die erste, nie die richtige ID. – Joarte
Bitte aktualisieren Sie Ihre Frage und zeigen Sie uns die Entitäten Stadt und Laden. –