【问题标题】:Symfony2 Doctrine one to one mappingSymfony2 Doctrine 一对一映射
【发布时间】:2015-07-27 11:33:54
【问题描述】:

所以我试图在 Symfony2 中与 Doctrine 建立一对一的关系,但出现以下错误:

在渲染模板期间引发了异常 (“无法解析类的列“id”的类型 "IntoPeople\DatabaseBundle\Entity\Feedbackcycle"") 在 IntoPeopleDatabaseBundle:Feedbackcycle:index.html.twig 在第 65 行。

我有两个实体,Feedbackcycle 和 CDP。在反馈周期中,我有:

     /**
     * @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* }}

我会得到错误。

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案1】:

    已解决,我忘了在 CDP 中写 mappedBy:

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

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2022-08-10
      相关资源
      最近更新 更多