简体   繁体   中英

Symfony2 - Problems with mapping object in field form

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM