新的学说,尝试将具有ManyToOne
rel的对象插入到数据库中,但是会出错。在下面编写相关代码:
控制器:
$oPost = $this->getDoctrine()->getRepository('AppBundle:RedditPost')->findOneById(5);
$oComment = new \AppBundle\Entity\Comments();
$oComment->setComment('4');
$oComment->setCreated('2016-11-12');
$oPost->addComment($oComment);
$oEm = $this->getDoctrine()->getManager();
$oEm->persist($oPost);
$oEm->flush(); // -- getting error here
实体\ RedditPost:
public function __construct()
{
$this->comments = new ArrayCollection();
}
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Comments", mappedBy="post")
*/
protected $comments;
public function addComment(Comments $comments)
{
if (!$this->comments->contains($comments)) {
$comments->setPost($this);
$this->comments->add($comments);
}
return $this;
}
实体\评论:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\RedditPost", inversedBy="comments")
* @ORM\JoinColumn(name="post_id", referencedColumnName="id")
*/
protected $post;
public function setPost(RedditPost $post)
{
$this->post = $post;
return $this;
}
完整错误:
A new entity was found through the relationship 'AppBundle\Entity\RedditPost#comments' that was not configured to cascade persist operations for entity: AppBundle\Entity\Comments@000000005d78d3ae00000000b58ca229. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'AppBundle\Entity\Comments#__toString()' to get a clue.
我尝试并注意到:
,cascade={"persist"}
值添加到Comments实体,但错误根本没有改变$oEm->persist($oPost); $oEm->persist($oComment);
中进行2次持续调用 - 但我收到ERR_CONNECTION_RESET
错误,无法从那里进行调试。对此进行了新的尝试,结果仍然相同:
class Owner {
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Child", mappedBy="owner")
*/
protected $children;
public function addChild(Child $child)
{
if (!$this->children->contains($child)) {
$child->setOwner($this);
$this->children->add($child);
}
return $this;
}
}
class Child {
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Owner", inversedBy="children", cascade={"persist"})
* @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
*/
protected $owner;
/**
* @param mixed $owner
*/
public function setOwner(Owner $owner)
{
$this->owner = $owner;
return $this;
}
}
class DefaultController extends Controller
{
public function indexAction()
{
$owner = new Owner();
$child = new Child();
$child->setName('test');
$owner->addChild($child);
$em = $this->getDoctrine()->getManager();
$em->persist($owner);
$em->flush();
}
}
目前的解决方案是完全删除@ORM\OneToMany
,这样我就可以毫无问题地插入数据库。
答案 0 :(得分:0)
是的,你不应该从@ORM\OneToMany
到Post
Comment
。在这种情况下,您应该使用Unidirectional
关系而不是Bidirectional
,并处理来自Comment
实体(外键可用)的关系。您可能需要查看此blog以供参考。
那么,当你删除@ORM\OneToMany
关系时你松了什么。从字面上看,没什么。如果要为单个Comments
检索Post
列表,请编写存储库并使用它。
答案 1 :(得分:0)
发现问题..
我的表中有一个时间戳字段,我尝试这样设置:
$oComment->setCreated('2016-01-01');
这是我无法使用$em->persist($oComment)
的原因(它返回了ERR_CONNECTION_RESET错误)。
解决方案是$oComment->setCreated(new \DateTime('2016-01-01'));
。
这个ERR_CONNECTION_RESET错误没有显示任何内容,这使得debuging更加困难。