Symfony ORM完整性约束问题

Symfony ORM完整性约束问题,symfony,orm,Symfony,Orm,我正朝着以下方向努力: 一篇文章有许多评论 一篇评论只有一篇文章 在这一分钟,我成功地不允许用户在数据库中插入注释,该数据库链接到一个不存在的文章Id 当我试图通过内部联接打印出表时,问题就出现了。我收到错误信息: Notice: Undefined index: Article 这是我控制器中的代码 $repository = $this->getDoctrine()->getRepository('AppBundle:Article'); $query =

我正朝着以下方向努力:

一篇文章有许多评论

一篇评论只有一篇文章

在这一分钟,我成功地不允许用户在数据库中插入注释,该数据库链接到一个不存在的文章Id

当我试图通过内部联接打印出表时,问题就出现了。我收到错误信息:

 Notice: Undefined index: Article
这是我控制器中的代码

    $repository = $this->getDoctrine()->getRepository('AppBundle:Article');

    $query = $repository->createQueryBuilder('a')
        ->innerJoin('a.comments','c')
        ->where('a.title LIKE :phrase')
        ->setParameter(':phrase','hello')
        ->getQuery();
下面是实体类-非常感谢您的帮助

文章实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity
 * @ORM\Table(name="article")
*/
class Article {

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=100)
 * @Assert\NotBlank()
 */
protected $title;


 /**
 * @ORM\OneToMany(targetEntity="Comment", mappedBy="Article") << ERROR HERE I BELEIVE
 */
protected $comments;

/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

/**
 * @param mixed $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return mixed
 */
public function getTitle()
{
    return $this->title;
}

/**
 * @param mixed $title
 */
public function setTitle($title)
{
    $this->title = $title;
}

/**
 * @return mixed
 */
public function getComments()
{
    return $this->comments;
}

/**
 * @param mixed $comments
 */
public function setComments($comments)
{
    $this->comments = $comments;
}
  <?php

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
 use AppBundle\Entity\Article;


 /**
 * @ORM\Entity
 * @ORM\Table(name="comment")
 */
  class Comment {

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=300)
 */
protected $text;

/**
 * @ORM\Column(type="integer")
 * @ORM\ManyToOne(targetEntity="article", inversedBy="comment")
 * @ORM\JoinColumn(name="article_id", referencedColumnName="id", onDelete="CASCADE")
 */
protected $article_id;

/**
 * @return mixed
 */
public function getId()
{
    return $this->id;
}

/**
 * @param mixed $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return mixed
 */
public function getText()
{
    return $this->text;
}

/**
 * @param mixed $text
 */
public function setText($text)
{
    $this->text = $text;
}

/**
 * @return mixed
 */
public function getArticleId()
{
    return $this->article_id;
}

/**
 * @param mixed $article_id
 */
public function setArticleId($article_id)
{
    $this->article_id = $article_id;
}

}

您的实体中有两个小错误

您可以通过以下方式更正它们:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity
 * @ORM\Table(name="article")
 */
class Article
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="article")
     */
    protected $comments;
}
以下是我所改变的:

  • mappedBy=“Article”
    mappedBy=“article”
    (是对方的实体属性名称,不带重音)
  • 受保护的$article\u id→ <代码>受保护的$物品(您的属性不是id,而是实体)
  • @ORM\manytone(targetEntity=“article”,inversedBy=“comment”)
    @ORM\ManyToOne(targetEntity=“Article”,inversedBy=“comments”)
    (它是您的
    文章
    实体中的
    comments
    属性(带“s”),目标实体的大写字母为A

您还应该在文章的
\uuu构造方法中将您的
注释初始化为
数组集合
。嗨,Michael,非常感谢您抽出时间。另外,我还有*@ORM\Column(type=“integer”)在Comment.php中的$article属性上,由于这个原因,它没有读取我的manytone和join列
/**
 * @ORM\Entity
 * @ORM\Table(name="comment")
 */
class Comment
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Article", inversedBy="comments")
     * @ORM\JoinColumn(name="article_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $article;
}