Over Killer
Over Killer

Reputation: 513

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;
    }
}

Upvotes: 0

Views: 7781

Answers (1)

Fabien Salles
Fabien Salles

Reputation: 1191

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.

Upvotes: 1

Related Questions