douliao7354
2017-10-25 12:21
采纳率: 0%
浏览 48
已采纳

Symfony 3 Doctrine undefined index

I'm trying to fetch an object from MySQL database using doctrine. It has one-to-one relation with other one. I'm getting this error: Notice: Undefined index: id What I should correct to make this working? My fetch code is very simple, I try to fetch all objects:

    $emArt = $this->getDoctrine()->getRepository(Article::class);
    $articles = $emArt->findAll();

Model is as following:

Article.php

    /**
     * @ORM\Entity
     * @ORM\Table(name="Article")
     */
    class Article
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $ID;
        /**
         * @ORM\OneToOne(targetEntity="ArticleTypes", inversedBy="articleTypeId")
         * @ORM\JoinColumn(name="articleTypeId", referencedColumnName="id")
         */
        private $articleType;
        /**
         * @ORM\Column(name="articleName", type="text")
         */
        private $articleName;
        /**
         * @ORM\Column(name="content", type="text")
         */
        private $content;

        /**
         * @ORM\Column(name="image", type="string")
         * @Assert\File(mimeTypes={ "image/png" })
         */
        public $image;

        public function getArticleImage()
        {
            return $this->image;
        }

        public function setArticleImage($newImage)
        {
            $this->image = $newImage;
        }

        public function getArticleContent()
        {
            return $this->content;
        }

        public function setArticleContent($name)
        {
            $this->content = $name;
        }

        public function getArticleName()
        {
            return $this->articleName;
        }

        public function setArticleName($name)
        {
            $this->articleName = $name;
        }

        public function getArticleType()
        {
            return $this->articleType;
        }

        public function setArticleType($type)
        {
            $this->articleType = $type;
        }

    }

ArticleTypes.php

/**
 * @ORM\Entity
 * @ORM\Table(name="ArticleTypes")
 */
class ArticleTypes
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToOne(targetEntity="Article", mappedBy="articleTypeId")
     */
    private $ID;
    /**
     * @ORM\Column(name="articleType", type="text")
     */
    private $articleType;

    public function getArticleType()
    {
        return $this->articleType;
    }
    public function setArticleType($newType)
    {
        $this->articleType = $newType;
    }
}

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dqxmf02844 2017-10-25 12:44
    最佳回答

    It's a PHP Notice no ? You should have also the line number in a file with the error ?

    But I saw errors in your doctrine mapping too : You can't use doctrine annotation like this with fields, you should link your relationship to objects :

     /**
     * @ORM\Entity
     * @ORM\Table(name="Article")
     */
    class Article
    {
        /**
         * @ORM\OneToOne(targetEntity="ArticleTypes", inversedBy="article")
         * @ORM\JoinColumn(name="articleTypeId", referencedColumnName="id")
         */
        private $articleType;
        ....
    }
    

    And :

    /**
     * @ORM\Entity
     * @ORM\Table(name="ArticleTypes")
     */
    class ArticleTypes
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /** 
         * @var Article $article
         * @ORM\OneToOne(targetEntity="Article", mappedBy="articleType")
         */
        private $article;
    
        ....
    }
    

    Even if you have a one to one relationship you can have different ids.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题