我的学说协会不能正常工作
my Doctrine association does not work properly
嗨,
我想要实现的目标:我有项目,在项目编辑中,有一个表格,我可以在其中放置图像(dropzone.js),这些图像被保存并分配给给定的项目.
图片上传成功,图片实体正在保存到图片中 table 并且它们拥有权利 project_id。但是如果我访问项目实体,"images" 在项目数组中是 "null"。不是图像实体的集合。
它看起来像一个没有默认值的简单私有变量。
我想我的 OneToMany 和 ManyToOne 关联似乎不起作用。
一些代码:
Project.php
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Image", mappedBy="project", cascade={"persist, remove"})
*/
private $images;
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
public function addImage(Image $image)
{
$this->images[] = $image;
}
public function removeImage(Image $image) {
$this->images->removeElement($image);
}
Image.php
/**
* @var \AppBundle\Entity\Project
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Project", inversedBy="images")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="project_id", referencedColumnName="id")
* })
*/
private $project;
/**
* Set project
*
* @param \AppBundle\Entity\Project $project
*
* @return Image
*/
public function setProject(\AppBundle\Entity\Project $project = null)
{
$this->project = $project;
return $this;
}
/**
* Get project
*
* @return \AppBundle\Entity\Project
*/
public function getProject()
{
return $this->project;
}
一切都保存到数据库
但图像是 "null"(不是 ArrayCollection :( )
或许,是少了什么。但我不知何故看不到它(尽管它太基本了)
干杯
阿德里安
在Project.php
中执行以下操作
在构造函数中,添加以下内容:
public function __construct()
{
// ...
$this->images = new ArrayCollection();
}
*将 addImage 函数更改为:
public function addImage(Image $image)
{
if(!$this-images->contains($image))
$this-images->add(image);
}
然后在 Image.php 中,将 setProject 更改为:
public function setProject(\AppBundle\Entity\Project $project = null)
{
$this->project = $project;
if($project != null)
$project->addImage($this);
return $this;
}
坚持一些图像并检查"Images"是否不为空。
我同意 Medard 关于构造函数的观点,您也可以尝试在 oneToMany 注释上将 fetch 参数设置为惰性。
paramConverter 可能有误
天哪...
当我从 DB 生成实体时(使用 doctrine:generate),在生成过程的中间有映射文件(参见 http://symfony.com/doc/current/doctrine/reverse_engineering.html)
我一删除 src/AppBundle/Resources/config/doctrine 文件夹(里面有 orm.config.xml 个文件),图像就显示为持久的 ArrayCollection。
但结果仍然是空的。
所以我不得不另外将 fetch="EAGER" 放入 OneToMany 映射中,因为延迟加载似乎无法正常工作(在 twig 中倾倒时,结果 project.images 未初始化)
非常感谢您的帮助。最后,我的图像正确显示。
如果 symfony 会在 dev.log 中写入映射错误就好了,这样我就不用搜索 2 天了。
赛亚
阿德里安
嗨,
我想要实现的目标:我有项目,在项目编辑中,有一个表格,我可以在其中放置图像(dropzone.js),这些图像被保存并分配给给定的项目.
图片上传成功,图片实体正在保存到图片中 table 并且它们拥有权利 project_id。但是如果我访问项目实体,"images" 在项目数组中是 "null"。不是图像实体的集合。 它看起来像一个没有默认值的简单私有变量。 我想我的 OneToMany 和 ManyToOne 关联似乎不起作用。
一些代码:
Project.php
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Image", mappedBy="project", cascade={"persist, remove"})
*/
private $images;
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
public function addImage(Image $image)
{
$this->images[] = $image;
}
public function removeImage(Image $image) {
$this->images->removeElement($image);
}
Image.php
/**
* @var \AppBundle\Entity\Project
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Project", inversedBy="images")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="project_id", referencedColumnName="id")
* })
*/
private $project;
/**
* Set project
*
* @param \AppBundle\Entity\Project $project
*
* @return Image
*/
public function setProject(\AppBundle\Entity\Project $project = null)
{
$this->project = $project;
return $this;
}
/**
* Get project
*
* @return \AppBundle\Entity\Project
*/
public function getProject()
{
return $this->project;
}
一切都保存到数据库
但图像是 "null"(不是 ArrayCollection :( )
或许,是少了什么。但我不知何故看不到它(尽管它太基本了)
干杯 阿德里安
在Project.php
中执行以下操作在构造函数中,添加以下内容:
public function __construct()
{
// ...
$this->images = new ArrayCollection();
}
*将 addImage 函数更改为:
public function addImage(Image $image)
{
if(!$this-images->contains($image))
$this-images->add(image);
}
然后在 Image.php 中,将 setProject 更改为:
public function setProject(\AppBundle\Entity\Project $project = null)
{
$this->project = $project;
if($project != null)
$project->addImage($this);
return $this;
}
坚持一些图像并检查"Images"是否不为空。
我同意 Medard 关于构造函数的观点,您也可以尝试在 oneToMany 注释上将 fetch 参数设置为惰性。
paramConverter 可能有误
天哪...
当我从 DB 生成实体时(使用 doctrine:generate),在生成过程的中间有映射文件(参见 http://symfony.com/doc/current/doctrine/reverse_engineering.html)
我一删除 src/AppBundle/Resources/config/doctrine 文件夹(里面有 orm.config.xml 个文件),图像就显示为持久的 ArrayCollection。
但结果仍然是空的。 所以我不得不另外将 fetch="EAGER" 放入 OneToMany 映射中,因为延迟加载似乎无法正常工作(在 twig 中倾倒时,结果 project.images 未初始化)
非常感谢您的帮助。最后,我的图像正确显示。
如果 symfony 会在 dev.log 中写入映射错误就好了,这样我就不用搜索 2 天了。
赛亚 阿德里安