Question

I want to use "entity" as field type in my form and display name of records in drop down list and save id of the selected choice to the database.

This is my FormType :

namespace Acme\DemoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Acme\DemoBundle\Entity\Person;

class ContactType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('subject')
            ->add('content')
            ->add('personid','entity', array(
                                        'class' => 'Acme\DemoBundle\Entity\Person',
                                        'query_builder' => function(EntityRepository $repository) {
                                                               return $repository->createQueryBuilder('q');
                                                            }))
        ;
    }


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

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

It loads list of records from database and displays them in the drop down list with correct options and values(id's of records) in tag in the html form, But does not save the value of the option (id of the record) to database.

If I use _toString(), Symfony does not display any error but saves "0" instead of id, and If I do not use _toString() and set "'property' => 'name',", it displays this error :

ContextErrorException: Catchable Fatal Error: Object of class <Path to Entity> could not be converted to string in /symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 855

I followed Symfony's documentation here : http://symfony.com/doc/current/reference/forms/types/entity.html but did not find the solution.

What is wrong ?

Was it helpful?

Solution 3

Solved !

For other friends who may have the same problem, I explain it here :

The error was in my entity relationship mapping.

I followed this documentation :

http://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata

and changed annotations and yml configs, then executed this command line :

php app/console doctrine:generate:entities Acme

Now it works fine !

OTHER TIPS

Please paste your whole form class, I can fix it for you. And meanwhile, you shouldn't be working with regular 'id's, you should work with associations instead.

UPDATED*

public function setPerson(Person $person)
{
    $this->person = $person;
    return $this;
}

Also make sure, you have your ORM set up correctly (Contact.orm.xml), like so for example:

<many-to-one field="person" target-entity="Your\Bundle\Entity\Person">
  <join-columns>
    <join-column name="idp" referenced-column-name="id"/>
  </join-columns>
</many-to-one>

The problem is in your Acme\DemoBundle\Entity\Person entity. The form needs somehow to represent each Person entity, so you need a magic method in it.

Please add this method to the entity:

public function __toString() {
  return $this->getName(); // You should return whatever field you feel is needed
}

this method is a magic method. Morei nformation can be found here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top