Symfony ORM完整性约束问题

时间:2015-06-04 16:17:31

标签: symfony orm

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

文章有很多评论

评论只有一篇文章

当我成功不允许用户在数据库中插入评论时,该评论链接到不存在的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;
}

}

1 个答案:

答案 0 :(得分:1)

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

你可以这样纠正:

<?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")(它是comments实体中的Article属性(带有“s”),目标实体具有大写字母A
相关问题