Symfony2 Doctrine一对一映射

时间:2015-07-27 11:33:55

标签: php symfony doctrine-orm doctrine

所以我试图在Symfony2中与Doctrine建立一对一的关系,但是我遇到了以下错误:

  

在渲染模板期间抛出了异常   (“无法解析类的列”id“类型   “IntoPeople \ DatabaseBundle \ Entity \ Feedbackcycle”“)中   IntoPeopleDatabaseBundle:Feedbackcycle:index.html.twig at the line 65。

我有两个实体,即Feedbackcycle和CDP。在Feedbackcycle中我有:

     /**
     * @var \IntoPeople\DatabaseBundle\Entity\Cdp
     *
     * @ORM\OneToOne(targetEntity="IntoPeople\DatabaseBundle\Entity\Cdp", inversedBy="feedbackcycle")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="CDPId", referencedColumnName="Id")
     * })
     */
    private $cdp;


     /**
     * Set cdp
     *
     * @param \IntoPeople\DatabaseBundle\Entity\Cdp $cdp
     *
     * @return Feedbackcycle
     */
    public function setCdp(\IntoPeople\DatabaseBundle\Entity\Cdp $cdp = null)
    {
        $this->cdp = $cdp;

        return $this;
    }

    /**
     * Get cdp
     *
     * @return \IntoPeople\DatabaseBundle\Entity\Cdp
     */
    public function getCdp()
    {
        return $this->cdp;
    }

在CDP中,我有:

     /**
     * @ORM\OneToOne(targetEntity="Feedbackcycle")
     */
    protected $feedbackcycle;

     /**
     * Set feedbackcycle
     *
     * @param \IntoPeople\DatabaseBundle\Entity\Feedbackcycle $feedbackcycle
     *
     * @return Cdp
     */
    public function setFeedbackcycle(\IntoPeople\DatabaseBundle\Entity\Feedbackcycle $feedbackcycle = null)
    {
        $this->feedbackcycle = $feedbackcycle;

        return $this;
    }

    /**
     * Get feedbackcycle
     *
     * @return \IntoPeople\DatabaseBundle\Entity\Feedbackcycle
     */
    public function getFeedbackcycle()
    {
        return $this->feedbackcycle;
    }

所以在我的树枝上,我可以做到这一点:

{{ feedbackcycle.cdp.id }}

这样可行,或者我也可以做feedbackcycle.name(任何属性),它会起作用。但是当我做的时候

{{ feedbackcycle.cdp.*ANOTHER ATTRIBUTE* }}

我会收到错误。

1 个答案:

答案 0 :(得分:0)

解决了,我忘了在CDP中写mapBy:

/**
 * @ORM\OneToOne(targetEntity="Feedbackcycle", mappedBy="cdp")
 */
protected $feedbackcycle;