Php 第二条原则,一家之家从未有人居住
我和一对夫妻的关系很难相处,但我的收藏中从来没有这样的信条 以下是我的课程:Php 第二条原则,一家之家从未有人居住,php,doctrine-orm,one-to-many,symfony,Php,Doctrine Orm,One To Many,Symfony,我和一对夫妻的关系很难相处,但我的收藏中从来没有这样的信条 以下是我的课程: class User { ... /** * @ORM\OneToMany(targetEntity="AppBundle\Entity\Post", mappedBy="idUser", cascade={"all"}) */ private $posts; public function getPosts() { return $this->
class User
{
...
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Post", mappedBy="idUser", cascade={"all"})
*/
private $posts;
public function getPosts() {
return $this->posts;
}
public function __construct() {
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}
...
}
class Post {
...
/**
* @var \AppBundle\Entity\User
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User" , inversedBy="posts" , cascade={"all"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_user", referencedColumnName="id")
* })
*/
private $idUser;
...
}
当我得到这样的用户对象时:
$user = $this->getDoctrine()
->getRepository('AppBundle:User')->find(1);
$user
已填充,但$posts
为null
另外,如果我执行$user->getPosts()
操作,它将返回null
为什么$posts
为空?它至少应该是一个数组
My DB有一个链接到用户1的帖子
如果我对Post
执行相同操作,则Post将自动填充关联用户
感谢您在博士后课程中的帮助,您有:
public function setUser($user){
$user->addPost($this);
$this->user= $user;
}
也许你需要这样的东西
在此之后编辑2
同样在用户类中,它应该是这样的:
private $posts = null;
...
/*
* Add posts to the array.
*/
public function addPost($post){
$this->stu_posts [] = $post;
}
在你的博士后课程中,你有:
public function setUser($user){
$user->addPost($this);
$this->user= $user;
}
也许你需要这样的东西
在此之后编辑2
同样在用户类中,它应该是这样的:
private $posts = null;
...
/*
* Add posts to the array.
*/
public function addPost($post){
$this->stu_posts [] = $post;
}
我添加了它,但仍然
posts
为空。我更新了我的帖子。可能你没有正确地安装它-检查它,就像我所说的。这应该行得通。如果你还有问题,请告诉我。还有问题。我认为这与循环依赖和阻止循环依赖有关。我添加了循环依赖,但仍然posts
为空。我更新了我的帖子。可能你没有正确地安装它-检查它,就像我所说的。这应该行得通。如果你还有问题,请告诉我。还有问题。我认为这与循环依赖有关,并防止它们