如何设置外键的id#sf2#doctrine2 [英] How to set the id of a foreign key id #sf2 #doctrine2
本文介绍了如何设置外键的id#sf2#doctrine2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试手动将外键ID设置为一个对象,但没有找到如何执行
I'm trying to manually set an foreign key id to an object, but didn't find how to do it
class Item
{
/**
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\ItemType", inversedBy="itemTypes")
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
*/
protected $item_type;
}
有没有办法做一些链接?
Is there a way to do something link that?
$item = new Item();
$item->setItemTypeId(1); // This generate an error.
还是要这样做?
$item = new Item();
$type = Repository::RetrieveById(1);
$item->setItemType($type); // This generate an error.
推荐答案
可以使用参考代理,让您获得对标识符已知的实体的引用,而不从数据库加载该实体。
This can be done using Reference Proxies, which let you obtain a reference to an entity for which the identifier is known, without loading that entity from the database.
$type = $em->getReference('MyBundle\Entity\ItemType', 1);
$item->setItemType($type);
这篇关于如何设置外键的id#sf2#doctrine2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文