[英]Foreign key not saving with OneToMany relationship in Doctrine/Symfony
Here are my two entity classes: 这是我的两个实体类:
<?php
namespace IW\Bundle\ArticlesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="article")
*/
class Article
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="article")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $heading;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $body;
/**
* @ORM\Column(type="datetime", nullable=false)
* @ORM\Version
* @var \DateTime
*/
protected $created = null;
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="article", cascade="persist")
*/
private $comments;
// non persisted
protected $username;
protected $user_id;
public function setUsername($username) {
$this->username = $username;
}
public function getUsername() {
return $this->username;
}
public function setUserId($id) {
$this->user_id = $id;
}
public function getUserId() {
return $this->user_id;
}
// end non persisted
// can this be hooked into event? i.e. JMSSerialiser?
public function beforeSerialisation() {
$this->setUserid($this->user->getId());
$this->setUsername($this->user->getUsername());
$this->setUser(null);
}
public function __construct() {
$this->created = new \DateTime();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set heading
*
* @param string $heading
* @return Article
*/
public function setHeading($heading)
{
$this->heading = $heading;
return $this;
}
/**
* Get heading
*
* @return string
*/
public function getHeading()
{
return $this->heading;
}
/**
* Set body
*
* @param string $body
* @return Article
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* Set user
*
* @param \IW\Bundle\ArticlesBundle\Entity\User $user
* @return Article
*/
public function setUser(\IW\Bundle\ArticlesBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \IW\Bundle\ArticlesBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set created
*
* @param \DateTime $created
* @return Article
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Get comments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add comments
*
* @param \IW\Bundle\ArticlesBundle\Entity\Comment $comments
* @return Article
*/
public function addComment(\IW\Bundle\ArticlesBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
return $this;
}
/**
* Remove comments
*
* @param \IW\Bundle\ArticlesBundle\Entity\Comment $comments
*/
public function removeComment(\IW\Bundle\ArticlesBundle\Entity\Comment $comments)
{
$this->comments->removeElement($comments);
}
}
namespace IW\Bundle\ArticlesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="comment")
*/
class Comment
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $body;
/**
* @ORM\Column(type="datetime", nullable=false)
* @ORM\Version
* @var \DateTime
*/
protected $created = null;
/**
* @ORM\ManyToOne(targetEntity="Article", inversedBy="comments")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
*/
private $article;
public function __construct() {
$this->created = new \DateTime();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set body
*
* @param string $body
* @return Comment
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* Set created
*
* @param \DateTime $created
* @return Comment
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
// non persisted
/**
* @Assert\NotBlank()
* @Assert\Type(type="digit")
*/
protected $article_id;
public function setArticleId($id) {
$this->article_id = $id;
}
public function getArticleId() {
return $this->article_id;
}
// end non persisted
/**
* Set article
*
* @param \IW\Bundle\ArticlesBundle\Entity\Article $article
* @return Comment
*/
public function setArticle(\IW\Bundle\ArticlesBundle\Entity\Article $article = null)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* @return \IW\Bundle\ArticlesBundle\Entity\Article
*/
public function getArticle()
{
return $this->article;
}
}
Later in the code I am binding data to a form for comment, retrieving the article by id and calling $article->addComment($comment) and then calling: 稍后在代码中,我将数据绑定到表单以进行注释,通过id检索文章并调用$ article-> addComment($ comment),然后调用:
$this->entityManager->persist($article);
$this->entityManager->persist($comment);
$this->entityManager->flush();
The comment is saving but doesn't contain the reference/foreign key to article. 评论正在保存,但不包含文章的引用/外键。
You call addComment
on the Article but setArticle
is never called on your Comment. 您在Article上调用了
addComment
,但从未在Comment上调用setArticle
。 You can modify the setter addComment
in order to make it call setArticle
: 您可以修改设置器
addComment
,以使其调用setArticle
:
/**
* Add comments
*
* @param \IW\Bundle\ArticlesBundle\Entity\Comment $comments
* @return Article
*/
public function addComment(\IW\Bundle\ArticlesBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
$comments->setArticle($this);
return $this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.