Vladimir Hidalgo
Vladimir Hidalgo

Reputation: 708

Symfony2 form field type entity not setting selected option on form edit

I've the following FormBuilder in a Form/*Type.php class:

<?php
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tpmTargetId', null, array('label' => 'Target', 'required' => true))

        ->add('tpmSourcePropertyId', 'entity', array( 'label' => 'Global property', 'class' => 'TI\ML\SyncBundle\Entity\MlProperties'))

    ;
}

The problem is with the field tpmSourcePropertyId, when creating a new entry it displays the correct options in the dropdown field and when saving it inserts the correct ID but when trying to edit that entry the dropdown defaults to option #1 instead of the saved ID.

If I render the field a type text it shows the correct ID stored.

I've seem this question but I seem to be already using the proposed solution.

What could I do to debug this? Thank you!

--------- UPDATED

\config\doctrine\MlTargets.orm.yml

    tarEmpPropertyId:
        type: integer
        nullable: false
        unsigned: true
        comment: ''
        column: tar_emp_property_id

Entity\MlTargets.php

 /**
 * @var integer
 */
private $tarEmpPropertyId;


/**
 * Set tarEmpPropertyId
 *
 * @param integer $tarEmpPropertyId
 * @return MlTargets
 */
public function setTarEmpPropertyId($tarEmpPropertyId)
{
    $this->tarEmpPropertyId = $tarEmpPropertyId;

    return $this;
}

/**
 * Get tarEmpPropertyId
 *
 * @return integer 
 */
public function getTarEmpPropertyId()
{
    return $this->tarEmpPropertyId;
}

Upvotes: 1

Views: 1138

Answers (1)

rLinhares
rLinhares

Reputation: 367

When you declare a property, you mean to reference an object.

On the database, the column will save the id of the object, but symfony will load the reference of it and you will manipulate an object.

So you can try to

->add('tpmSourceProperty', 'entity', array('label' => 'Global property', 'class' =>'TI\ML\SyncBundle\Entity\MlProperties'))

Upvotes: 2

Related Questions