2015-06-04 91 views
2

我正在努力實現以下設置;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; 
} 

} 
+0

你還應該在文章的__construct方法中將你的'comments'初始化爲'ArrayCollection'。 – xurshid29

回答

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
+0

嗨邁克爾,非常感謝你的時間,非常感謝。此外,我還在Comment.php的$ article屬性中添加了* @ORM \ Column(type =「integer」),因爲它沒有讀取我的ManyToOne和JoinColumn – jmack

+0

不客氣!我用你最後的評論更新了我的答案。 –