Symfony2 Catchable Fatal Error: Argument 1 passed to entity Catchable Fatal Error: Argument 1 passed to entity

1.9k views Asked by At

I got this error and I'm stuck since many hours

Catchable Fatal Error: Argument 1 passed to Thinking\ThinkBundle\Entity\InYourMind::setThinkFriend() must be an instance of Thinking\ThinkBundle\Entity\InYourMindFriend, array given, called in /var/www/html/thinkroulette/vars/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined 

I searched for this issue and lots of results came up but I wasn't capable to solve this issue.

I'm trying to integrate two entities in one form and I got my idea form this post Create 2 different entities with same form in Symfony 2

Probably it's a silliness but I couldn't understood even the error. :(

following a snipped of the main form

//InYourMindType.php
    class InYourMindType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('thinkFriend', new InYourMindFriendType)
        //...

This is the "embedded" form:

//InYourMindDriendType  
class InYourMindFriendType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('email', 'email', array(
                'label' => 'label_think_userEmail',
                'required' => false,
                'attr' => array(
                    'title' => 'label_think_userEmail',
                    'class' => 'form-control'
                )
            ));
        }
    public function getName()
    {
        return 'thinkFriend';
    }

The twig where the main form is called (then the embedded one)

<div class="form-group">
    {{ form_label(t_form.title) }}
    {{ form_widget(t_form.title) }}
    {{ form_errors(t_form.title) }}
</div>
<div class="form-group">
    {{ form_label(t_form.thinkFriend.email) }}
    {{ form_widget(t_form.thinkFriend.email) }}
    {{ form_errors(t_form.thinkFriend.email) }}

Those forms belong to two different entities connected respectively by a many-to-one and one-to-many relationship.

If you need i post the entities just ask.

Thanks a lot

1

There are 1 answers

2
Tuan nguyen On BEST ANSWER

Set data_class option for your InYourMindFriendType

Checkout http://symfony.com/doc/current/reference/forms/types/form.html#data-class