1

I am trying to create a form with in a drop down list. The data are get from the entity. The problem is that I am getting a Expected argument of type "object", "integer" given exception.

Here is how I am trying to populate the dropdownlist in the form:

public function buildForm(FormBuilder $builder, array $options)
{
   $builder->add('country', 'entity', array(
        'class' => 'SciForumVersion2Bundle:Country',
        'property' => 'country',
    ));
}

In my Entity country, I have

/**
 * @ORM\Column(type="text")
 */
protected $country;

The object I am editing in the form is the user object:

$enquiry = $this->get('security.context')->getToken()->getUser();

In the user entity, I have

/**
 * @ORM\Column(type="integer")
 */
protected $country;

I don't know why I am getting this error.

1 Answer 1

2

There seem to be a problem in your model design, the user's "Country" property should be a Many-To-One association, not an integer (this is why the form builder complains) :

/**
 * @ORM\ManyToOne(targetEntity="Country")
 * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
 **/
private $country;

The "property" option is only used to display the entity choice to the user, Symfony2 uses the first parameter of the "add" method to decide which field of the object to edit.

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

5 Comments

Thank you Julien, I have tryed this but I am geting another exception: AnnotationException: [Semantical Error] The annotation "@ManyToOne" in property SciForum\Version2Bundle\Entity\User::$country was never imported. Did you maybe forget to add a "use" statement for this annotation?
It needs to use @ORM\ManyToOne and @ORM\JoinColumn in your model, assuming you have ORM in your use statements.
Exactly, I thought of it but I forgot to change it in the code, my bad. Symfony recommends to import the doctrine routes as @ORM, so you need to prefix the routes. I've edited my answer.
Thank you very much, I am getting the country id now. One more auestion if possible :). I would like to set the country id as value and the country name as text for my drop down list. Is it possible with the configuration I have actually and if so, do you please can tell me how can I do it?
If I understand your new requirement, it should already be the case. Could you add it to your question (or create a new one), explaining what you have and what you expect ?

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.