Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Exibir campos específicos do banco de dados em campos de valor e texto de uma combobox no Symfony2


Para exibir o nome da empresa em vez de seu ID, use o property opção (para Symfony 2.6 e abaixo) ou o choice_label opção (para Symfony 2.7+):

Para Symfony 2.6 e abaixo:
<?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';
    }
}

Para Symfony 2.7 e seguintes:
<?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';
    }
}

E a propósito, em seu Twig em vez de
{{ form_label(form.company) }}
{{ form_widget(form.company) }}

você pode escrever form_row(form.company) .