简体   繁体   中英

Field Type entity and current value in the database Symfony2

I'm working with Symfony2. I have two entities like below:

class User
{
    private $idUser;
    private $name;
    private $country;
    ...
}

class Country
{
    private $idCountry;
    private $namecountry;
    ...
}

And I create a form like this:

class UserType extends AbstractType
{
     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name','text')
            ->add('country', 'entity', array(
                'required' => false,
                'label' => 'Country',
                'class' => 'TestBundle:Country',
                'property' => 'namecountry',
                'empty_value' => false,
            ));
    }

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

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

Everything is okay, but suppose I want to modify a user who:

  • Lives in Canada
  • Name is Jack.

My problem occurs whenever the form appears, the field name contains the current value in the database but unlike the country field, it does not contain the correct value in the database. How could this be?

Could you show us the configuration for the relationship that you've defined between User and Country?

What you have should work but only if the relationship is set up correctly between the two entities. Otherwise, it won't match correctly to set the selected value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM