【问题标题】:Symfony ORM Integrity constraint issueSymfony ORM 完整性约束问题
【发布时间】:2015-06-04 16:17:30
【问题描述】:

我正在努力进行以下设置;

一篇文章有很多评论

一个评论只有一篇文章

目前我成功地不允许用户将评论插入到链接到不存在的 Article_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;
}

}

【问题讨论】:

  • 您还应该在文章的__construct 方法中将您的comments 初始化为ArrayCollection

标签: symfony orm


【解决方案1】:

您的实体中有 2 个小错误。

你可以这样改正它们:

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

和:

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

以下是我所做的更改:

  • mappedBy="Article"mappedBy="article"(是对方的实体属性名,不带重音)
  • protected $article_id;protected $article;(你的属性不是id,而是实体)
  • @ORM\ManyToOne(targetEntity="article", inversedBy="comment")@ORM\ManyToOne(targetEntity="Article", inversedBy="comments")(它是您在Article 实体中拥有的comments 属性(带有“s”),并且目标实体有一个大写的A

【讨论】:

  • 嗨迈克尔,非常感谢您抽出宝贵的时间,非常感谢。另外要补充的是,我在 Comment.php 的 $article 属性上也有 * @ORM\Column(type="integer"),因此它没有读取我的 ManyToOne 和 JoinColumn
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2012-07-11
相关资源
最近更新 更多