简体   繁体   中英

Symfony2 Doctrine one to one mapping

So I'm trying to make a one to one relationship in Symfony2 with Doctrine but I'm getting following error:

An exception has been thrown during the rendering of a template ("Could not resolve type of column "id" of class "IntoPeople\\DatabaseBundle\\Entity\\Feedbackcycle"") in IntoPeopleDatabaseBundle:Feedbackcycle:index.html.twig at line 65.

I have two entities, Feedbackcycle and CDP. In Feedbackcycle i have:

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

And in CDP I have:

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

So in my twig I can forexample do this:

{{ feedbackcycle.cdp.id }}

And that will work, or I can also do feedbackcycle.name ( any attribute ) and it will work. But when I do

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

I will get the error.

Solved, I forgot to write mappedBy in CDP:

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

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