我的数据库中有两个表:sliders
和images
。一个滑块可以有很多图像,因此表的结构是:
--------- --------
| SLIDERS | | IMAGES |
--------- --------
| id | | id |
--------- --------
| title | | title |
--------- --------
| sid |
--------
SID is a foreign key associated to id in "SLIDERS" table.
在实体中,我设置了双向关系OneToMany
和ManyToOne
,因此获取的Slider结果将包含属于他的Images Objects
,而图片将包含属于他们的Slider Object
滑块实体:
class Sliders
{
...
/**
* @ORM\OneToMany(targetEntity="Images", mappedBy="slider")
*/
protected $images;
图像实体:
class Images
{
...
/**
* @ORM\ManyToOne(targetEntity="Sliders", inversedBy="images")
* @ORM\JoinColumn(name="sid", referencedColumnName="id")
*/
protected $slider;
抓取真的很舒服。但现在我无法理解我如何INSERT
sid
进入Images
表,因为除了返回Object的$slider
之外,我的实体中没有此字段。是否可以使用此$slider
?
答案 0 :(得分:0)
以下功能您应该已经在Slider实体(或类似)中。
public function addImage(Image $image)
{
$image->setSlider($this); // This is the line you're probably looking for
$this->images[] = $image;
return $this;
}
它的作用是如果你坚持实体,它会将Slider(sid)的ID写入你的图像。