Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Zeigen Sie bestimmte Felder aus der Datenbank in Wert- und Textfeldern einer Combobox in Symfony2 an

Um den Firmennamen anstelle seiner ID anzuzeigen, verwenden Sie den Code <>Eigenschaft Möglichkeit (für Symfony 2.6 und unten) oder den choice_label Möglichkeit (für Symfony 2.7+):

Für Symfony 2.6 und unten:

<?php

namespace Benerite\CompanyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DepartmentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('company', 'entity', array(
                'class' => 'CompanyBundle:Company',
                'property' => 'companyName',
            ))  
            ->add('departmentName')
            ->add('departmentStatus')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Benerite\CompanyBundle\Entity\Department'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'benerite_companybundle_department';
    }
}

Für Symfony 2.7 und höher:

<?php

namespace Benerite\CompanyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DepartmentType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('company', 'entity', array(
                'class' => 'CompanyBundle:Company',
                'choice_label' => 'companyName',
            ))  
            ->add('departmentName')
            ->add('departmentStatus')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Benerite\CompanyBundle\Entity\Department'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'benerite_companybundle_department';
    }
}

Und übrigens in deinem Twig statt

{{ form_label(form.company) }}
{{ form_widget(form.company) }}

Sie können form_row(form.company) schreiben .