Symfony2 - 以字段形式映射对象的问题

时间:2014-06-08 23:46:47

标签: php forms symfony doctrine-orm

Objective有一个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使用带有所有视角的组合框渲染表单。那没问题!但当我提交create form时,我收到错误:

  

ContextErrorException:Catchable Fatal Error:传递给的参数1   Cboujon \ BSCBundle \ Entity \ Objective :: setPerspective()必须是一个   Cboujon的实例\ BSCBundle \ Entity \ Pespective,实例   给出了Cboujon \ BSCBundle \ Entity \ Perspective,调用   /home/cristhian/php_apps/Symfony/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php   在第376行并在中定义   /home/cristhian/php_apps/Symfony/src/Cboujon/BSCBundle/Entity/Objective.php   第63行

注意:我也尝试:

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

但我得到同样的错误。

注意2:如果我从setPerspective定义中删除\Cboujon\BSCBundle\Entity\Pespective,我可以提交表单确定。

我做错了什么?

1 个答案:

答案 0 :(得分:5)

在指定参数类型时,iThere是您的实体设置器中的拼写错误:

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

    return $this;
}

不是\ Cboujon \ BSCBundle \ Entity \ Pespective ,但\ Cboujon \ BSCBundle \ Entity \ 透视