0

I'm trying to create a simple form to add companies and i've a trouble using an entity.

I added a select field using a company type entity :

->add('idtypesociete', 'entity', array('class' => 'PromocastUtilisateurBundle:PcastTypesociete', 'property' => 'nomtypesociete'))

But when i submit the form my idtypesociete field contain an 'PcastTypesociete' object and not just the value of the option selected. So the submission fail.

I made a Many-To-One relation between my company entity and my typeCompany entity like this:

/**
 * @var integer $idtypesociete
 *
 * @ORM\Column(name="IDTYPESOCIETE", type="integer", nullable=false)
 * @ORM\ManyToOne(targetEntity="Promocast\UtilisateurBundle\Entity\PcastTypesociete")
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="PcastTypesociete_idtypesociete", referencedColumnName="idtypesociete")
 * })
 */
private $idtypesociete;

Do you have a solution to get only the id of the company type selected? (if possible without made a simple sql request to list my companies types)

Thanks a lot !

1 Answer 1

1

If the relationships are working then Symfony 2 usually does a very good job of building the form fields for you.

I think the issue is the $idtypesociete property. Are you expecting to store an integer here on the hydrated entity?

Doctrine associations use Entity relationships. The annotations you supply determine the behind-the-scenes stuff like the join column: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional

I suggest backing up or committing your work before doing anything else.

Does changing the entity property to the following help?

/**
 * @var PcastTypesociete $typesociete
 *
 * @ORM\Column(name="IDTYPESOCIETE", type="integer", nullable=false)
 * @ORM\ManyToOne(targetEntity="Promocast\UtilisateurBundle\Entity\PcastTypesociete")
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="PcastTypesociete_idtypesociete", referencedColumnName="idtypesociete")
 * })
 */
private $typesociete;

You may need to update your database schema via doctrine:schema:update using the console if it doesn't work properly the first time. Your Entity will also need to be updated to reflect the new property name.

If that works then your form should only need ->add('typesociete') in the form type and you'll have a functioning entity select field because Symfony is clever enough to know what field type to use.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for being late but yes i finally manage to do it ! Thanks a lot for your help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.