简体   繁体   English

Symfony 3主义未定义索引

[英]Symfony 3 Doctrine undefined index

I'm trying to fetch an object from MySQL database using doctrine. 我正在尝试使用该学说从MySQL数据库中获取一个对象。 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? 我收到此错误: Notice: Undefined index: id我应该纠正些什么才能使它正常工作? 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 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 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; } } 

It's a PHP Notice no ? 这是PHP声明,不是吗? 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. 即使您具有一对一的关系,您也可以具有不同的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM