我试图保存一个实体,可能或可能不存在,可能或可能不会有一个或两个关系(代码如下)。我目前的方法会导致错误,我很确定我可以自己提出一些解决方法/黑客攻击,但我对正确的“官方”方式感兴趣。学说2保存实体与可能的关系
我现在想:
$entity = new myEntity();
if (!empty($id))
{
$entity->setId($id);
}
$entity->setLocationId($relation_id); //may or may not be null, if not null it's always an already existing location entry in a different table, i.e. not a new
$entity = $entity_manager->merge($entity);
$entity_manager->flush();
目前主义抱怨位置是一个新的实体,没有ID和政策不允许自动生成的ID。策略确实是这样的,但我根本不添加Location实体,我使用setLocationId()自动生成的方法来添加精确地存在的位置ID,所以我有点困惑。
编辑:当位置标识符不为空而是一个真实的,存在的(以db为单位)位置的id时,我得到了doctrine错误。
和模型:
Location:
type: entity
table: locationstable
id:
my-id-column:
type: string
fields:
some fields
oneToMany:
myEntities:
targetEntity: myEntity
mappedBy: location
cascade: ["persist", "merge"]
谢谢,getReference()可能正是我所需要的!另外感谢合并废话的提示:)使用原则2是所有这个问题。我只需要知道什么不是什么。至于ID,我知道应该是什么ID,因为我生成了它们。 – Sejanus