I need to insert an entity which has associations.
我需要插入一个具有关联的实体。
If I already have the FK's of the associated entities, is there a way to insert the primary entity into the db with just the FK's populated?
如果我已经拥有关联实体的FK,是否有办法将主要实体插入到只填充FK的数据库中?
Or do I always have to
或者我总是必须这样做
- retrieve the associated entities via the FK's,
- 通过FK检索关联的实体,
- populate the primary entity's properties referring to the assocations,
- 填充主要实体的属性,引用关联,
- and then invoke the persist method.
- 然后调用persist方法。
4 个解决方案
#1
61
You want a reference proxy
你想要一个参考代理
Let's say I have Posts and Tags. A Post hasMany Tags. I get a bunch of tags from the user, who checked a bunch of checkboxes.
假设我有帖子和标签。帖子有很多标签。我从用户那里得到了一堆标签,他们检查了一堆复选框。
The following would add tags to an existing post, without fetching each tag entity first. It does this by using reference proxies, generated by EntityManager::getReference()
:
以下内容将向现有帖子添加标签,而不首先获取每个标签实体。它通过使用由EntityManager :: getReference()生成的引用代理来实现:
$tag_ids = $_POST['tag_id']; // an array of integers representing tag IDs.
$post = $em->getRepository('Post')->find($post_id); // returns a Post entity.
foreach($tags_ids as $tid){
$post->addTag($em->getReference('Tag',$tid));
}
$em->persist($post);
$em->flush();
#2
4
In regards to using a reference proxy
In my investigations this is only partly a solution, as follows:
关于使用参考代理在我的调查中,这只是部分解决方案,如下:
Yes, you do not have to pro-actively retrieve the related record (because you create a proxy record), but when you flush (commit) the update transaction it still first executes a select statement to retrieve the related record, and then only does the update (all in one hit to the db).
This is inefficient and should not be necessary (we have the foreign-key id, why retrieve the record..?)
是的,您不必主动检索相关记录(因为您创建了代理记录),但是当您刷新(提交)更新事务时,它仍然首先执行select语句来检索相关记录,然后只执行更新(全部合并到数据库)。这是低效的,不应该是必要的(我们有外键id,为什么要检索记录..?)
So while not a complete solution, what you do gain is only a single connection to the database (which is good) and slightly simplified code.
因此,虽然不是一个完整的解决方案,但您获得的只是与数据库的单一连接(这是好的)和略微简化的代码。
I am not sure if there is a solution to this at the moment...??
Hopefully the doctrine bods will update in the future and if using the proxy logic we should gain an automatic performance enhancement...
我不确定目前是否有解决方案......?希望将来更新学说标准,如果使用代理逻辑,我们应该获得自动性能提升......
#3
0
You should retrieve the entity to be related and the make the relationship.
您应该检索要关联的实体并建立关系。
I assume you could manually specify the relationship by directly accessing the database through the DBAL layer but I wouldn't not recommend this, nor have I tried it.
我假设您可以通过DBAL层直接访问数据库来手动指定关系,但我不会推荐这个,也没有尝试过。
#4
-1
You can do this using entityManager::merge
您可以使用entityManager :: merge执行此操作
$post = new Post();
$post->setPostCategory(['id' => 1]);
$em->persist($em->merge($post));
$em->flush();
#1
61
You want a reference proxy
你想要一个参考代理
Let's say I have Posts and Tags. A Post hasMany Tags. I get a bunch of tags from the user, who checked a bunch of checkboxes.
假设我有帖子和标签。帖子有很多标签。我从用户那里得到了一堆标签,他们检查了一堆复选框。
The following would add tags to an existing post, without fetching each tag entity first. It does this by using reference proxies, generated by EntityManager::getReference()
:
以下内容将向现有帖子添加标签,而不首先获取每个标签实体。它通过使用由EntityManager :: getReference()生成的引用代理来实现:
$tag_ids = $_POST['tag_id']; // an array of integers representing tag IDs.
$post = $em->getRepository('Post')->find($post_id); // returns a Post entity.
foreach($tags_ids as $tid){
$post->addTag($em->getReference('Tag',$tid));
}
$em->persist($post);
$em->flush();
#2
4
In regards to using a reference proxy
In my investigations this is only partly a solution, as follows:
关于使用参考代理在我的调查中,这只是部分解决方案,如下:
Yes, you do not have to pro-actively retrieve the related record (because you create a proxy record), but when you flush (commit) the update transaction it still first executes a select statement to retrieve the related record, and then only does the update (all in one hit to the db).
This is inefficient and should not be necessary (we have the foreign-key id, why retrieve the record..?)
是的,您不必主动检索相关记录(因为您创建了代理记录),但是当您刷新(提交)更新事务时,它仍然首先执行select语句来检索相关记录,然后只执行更新(全部合并到数据库)。这是低效的,不应该是必要的(我们有外键id,为什么要检索记录..?)
So while not a complete solution, what you do gain is only a single connection to the database (which is good) and slightly simplified code.
因此,虽然不是一个完整的解决方案,但您获得的只是与数据库的单一连接(这是好的)和略微简化的代码。
I am not sure if there is a solution to this at the moment...??
Hopefully the doctrine bods will update in the future and if using the proxy logic we should gain an automatic performance enhancement...
我不确定目前是否有解决方案......?希望将来更新学说标准,如果使用代理逻辑,我们应该获得自动性能提升......
#3
0
You should retrieve the entity to be related and the make the relationship.
您应该检索要关联的实体并建立关系。
I assume you could manually specify the relationship by directly accessing the database through the DBAL layer but I wouldn't not recommend this, nor have I tried it.
我假设您可以通过DBAL层直接访问数据库来手动指定关系,但我不会推荐这个,也没有尝试过。
#4
-1
You can do this using entityManager::merge
您可以使用entityManager :: merge执行此操作
$post = new Post();
$post->setPostCategory(['id' => 1]);
$em->persist($em->merge($post));
$em->flush();