Mysql
 sql >> Database >  >> RDS >> Mysql

Visualizza campi specifici dal database in valori e campi di testo di una casella combinata in Symfony2

Per visualizzare il nome dell'azienda invece del suo ID, utilizzare il property opzione (per Symfony 2.6 e seguenti) o il choice_label opzione (per Symfony 2.7+) :

Per Symfony 2.6 e versioni successive:

<?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';
    }
}

Per Symfony 2.7 e successivi:

<?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 proposito, nel tuo ramoscello invece di

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

puoi scrivere form_row(form.company) .