0
votes

Objective has one Perspective

class Objective{
    ...
    public function setPerspective(\Cboujon\BSCBundle\Entity\Pespective $perspective = null)
    {
        $this->perspective = $perspective;

        return $this;
    }
}

Objective.orm.yml

Cboujon\BSCBundle\Entity\Objective:
manyToOne:
        perspective:
            targetEntity: Perspective
            inversedBy: objectives
            joinColumn:
                name: perspective_id
                referencesColumn: id

ObjectiveType.php

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('perspective');
    }

Symfony2 renders a form with combobox with all perspectives. That is ok! butn when I submit the create form I get the error:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Cboujon\BSCBundle\Entity\Objective::setPerspective() must be an instance of Cboujon\BSCBundle\Entity\Pespective, instance of Cboujon\BSCBundle\Entity\Perspective given, called in /home/cristhian/php_apps/Symfony/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 376 and defined in /home/cristhian/php_apps/Symfony/src/Cboujon/BSCBundle/Entity/Objective.php line 63

Note: I also try:

        $builder->add('perspective', 'entity', array(
                'class' => 'CboujonBSCBundle:Perspective',
                )
              )
    ;

But I get the same error.

Note2: If I remove \Cboujon\BSCBundle\Entity\Pespective from setPerspective definition I can submit the form OK.

What am I doing wrong?

1

1 Answers

5
votes

iThere is a typo in your Entity setter when specifying the argument type:

public function setPerspective(\Cboujon\BSCBundle\Entity\Pespective $perspective = null)
{
    $this->perspective = $perspective;

    return $this;
}

Not \Cboujon\BSCBundle\Entity\ Pespective, but \Cboujon\BSCBundle\Entity\ Perspective