1

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

1 Answer 1

2

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'))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, if I remove the Id part then I get errors about property not found. I've updated the question and added more info on the YML and Entity files. Thank you!.
i think you just need set the default option of your dropdown to be the loaded id.
Thank you, but how could I access the loaded id in that context? That would be nice!
maybe this link could be helpful.. this guy needs set a default value for his dropdown too.. stackoverflow.com/questions/18270513/…

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.