实体/ user.php的:
// ...
use Doctrine\Common\Collections\ArrayCollection;
// ...
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Picture", mappedBy="user")
* @ORM\OrderBy({"file" = "ASC"})
**/
private $pictures;
/**
* Constructor
*/
public function __construct()
{
$this->pictures = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add picture
*
* @param \MyBundle\Entity\Picture $picture
*
* @return User
*/
public function addPicture(\MyBundle\Entity\Picture $picture)
{
$this->pictures[] = $picture;
return $this;
}
/**
* Remove picture
*
* @param \MyBundle\Entity\Picture $picture
*/
public function removePicture(\MyBundle\Entity\Picture $picture)
{
$this->pictures->removeElement($picture);
}
/**
* Get pictures
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPictures()
{
return $this->pictures;
}
// ...
实体/ Picture.php
// ...
/**
* @var ArrayCollection
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="pictures")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $user;
/**
* Set user
*
* @param \MyBundle\Entity\User $user
*
* @return Picture
*/
public function setUser(\MyBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \MyBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
// ...
控制器/ PictureController.php:
// ...
public function indexAction(Request $request)
{
return array(
'pictures' => $this->getUser()->getPictures(),
);
}
// ...
当我转储结果 - > getPictures()时,这就是Symfony的探查器输出的内容:
PersistentCollection {#292 ▼
-snapshot: []
-owner: User {#270 ▶}
-association: array:16 [ …16]
-em: EntityManager {#53 …11}
-backRefFieldName: "user"
-typeClass: ClassMetadata {#272 …}
-isDirty: false
#collection: ArrayCollection {#293 ▶}
#initialized: false
}
不知怎的,集合没有初始化,也没有项目,虽然数据库中有一些......我在这里错过了什么点?