在两个实体之间创建关联时是否可以传递 id 而不是实体?
我有一个 Item
实体和一个 Category
实体。一个Item
有一个Category
。我的映射代码如下所示:
// Item.php
/**
* @ORM\ManyToOne(targetEntity = "Category")
* @ORM\JoinColumn(name = "category_id", referencedColumnName = "id")
*/
protected $category;
要创建关联,我使用此方法:
// Item.php
public function setCategory(Category $category) {
$this->category = $category;
}
只要我首先从数据库获取 Category
实体,该方法就可以正常工作。但我想知道是否可以传递 id 而不是 Category
实体。我想使用标量值手动设置 JoinColumn category_id
。但由于 category_id
不是 Item
的实际成员,我不确定如何做到这一点。
I have an Item
entity and a Category
entity. An Item
has one Category
. My mapping code looks like this:
// Item.php
/**
* @ORM\ManyToOne(targetEntity = "Category")
* @ORM\JoinColumn(name = "category_id", referencedColumnName = "id")
*/
protected $category;
To create the association, I use this method:
// Item.php
public function setCategory(Category $category) {
$this->category = $category;
}
This works fine as long as I first fetch the Category
entity from the DB. But I'm wondering if it's possible to pass an id instead of the Category
entity. I'd like to manually set the JoinColumn category_id
with a scalar value. But sine category_id
isn't an actual member of Item
, I'm not sure how I can do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
getReference
:Use
getReference
: