- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的目标是使用来自同一类但不受 Doctrine 管理的对象的数据对托管实体执行更新。
如果可以在替换属性时执行“简单更新”,这会很酷,但实际上,如果我清理 ArrayCollection
,旧数据似乎不会被删除(即使我从 ArrayCollection
的元素或如果 orphanRemoval
设置为 true 时清除所有对 fiddle 的引用。
但是让我们输入一个具体的例子。我有 this entity有很多 OneToOne/OneToMany 关系来表示 fiddle .我可以使用 Symfony2 命令导入 fiddle 样本(之前从另一个环境导出为 json)。
如果示例已经存在,我该如何正确更新它?
我使用以下代码(简化)构建我的实体:
$fiddle = new Fiddle();
$fiddle->setHash($this->get($json, 'hash'));
$fiddle->setRevision($this->get($json, 'revision'));
$context = $fiddle->getContext();
$context->setFormat($this->get($json, 'context', 'format'));
$context->setContent($this->get($json, 'context', 'content'));
$fiddle->clearTemplates();
$jsonTemplates = $this->get($json, 'templates') ? : array ();
foreach ($jsonTemplates as $jsonTemplate)
{
$template = new FiddleTemplate();
$template->setFilename($this->get($jsonTemplate, 'filename'));
$template->setContent($this->get($jsonTemplate, 'content'));
$template->setIsMain($this->get($jsonTemplate, 'is-main'));
$fiddle->addTemplate($template);
}
// ...
如果实体已经存在,我现在可以在删除实体后保留它:
$check = $this
->getContainer()
->get('doctrine')
->getRepository('FuzAppBundle:Fiddle')
->getFiddle($fiddle->getHash(), $fiddle->getRevision());
if (!is_null($check->getId()))
{
$em->remove($check);
$em->flush();
}
$em->persist($fiddle);
$em->flush();
但是如果样本已经存在,这将创建一个 DELETE + INSERT 而不是 UPDATE。这很奇怪,因为用户可以为 fiddle 添加书签,并且这种关系是由 id 建立的。
我先得到我的 fiddle ,如果它已经存在,我会清理它并用新数据填充它......代码运行良好但真的很丑,你可以检查它here .
作为示例,检查 tags
属性:由于标签可能已被删除/更改,我应该正确设置新标签,将旧标签替换为新标签。
// remove the old tags
foreach ($fiddle->getTags() as $tag)
{
if (\Doctrine\ORM\UnitOfWork::STATE_MANAGED === $em->getUnitOfWork()->getEntityState($tag))
{
$em->remove($tag);
$em->flush();
}
}
// set the new tags
$tags = new ArrayCollection();
$jsonTags = $this->getFromArray($json, 'tags');
foreach ($jsonTags as $jsonTag)
{
$tag = new FiddleTag();
$tag->setTag($jsonTag);
$tags->add($tag);
}
$fiddle->setTags($tags);
由于标签是使用 fiddle 的 id 引用的,所以我可以使用 ->remove
,即使那很丑陋。这在这里没问题,但如果 ID 是自动生成的,则必须有更好的解决方案。
我还尝试简单地将旧 fiddle 的 id 设置为新 fiddle 的 id 并合并,但这导致了以下异常:
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Undefined index: 00000000125168f2000000014b64e87f
不仅仅是一个简单的“导入功能”,我想使用这种更新样式将表单绑定(bind)到非托管实体并仅在需要时更新现有实体。所以我的目标是让一些通用的东西适用于所有类型的实体。
但我当然不期望整个代码。处理托管 ArrayCollection
更新的良好做法,以及一些关于我在编写此功能之前应该考虑的事项的提示/警告应该足够了。
最佳答案
Update existing entities only if required.
这可以通过 Doctrine 相当简单地实现:
您正在寻找的是变更跟踪政策 Deferred Explicit .
Doctrine 默认使用变更跟踪策略 Deferred Implicit .这意味着当你调用 $em->flush()
时,Doctrine 将遍历 所有 它的托管实体来计算变更集。然后保留所有更改。
使用更改跟踪策略时 Deferred Explicit并调用 $em->flush()
,Doctrine 将仅遍历您明确调用的实体 $em->persist()
在。换句话说:你可能有成千上万的托管实体,其中 2 个被称为 $em->persist()
,而 Doctrine 将只计算这 2 个的变更集(并在需要时持久化变更) ).
可以在实体类级别设置更改跟踪策略。所以如果你想让某个实体类使用Deferred Explicit ,只需向类文档 block 添加注释:
/**
* @Entity
* @ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Fiddle
{
然后只需在真正需要时调用 $em->persist($fiddle)
即可。
为整个聚合(根实体及其所有子实体)设置相同的更改跟踪策略可能是明智的。
PS:还有第三个更改跟踪策略,名为 Notify ,这需要做更多的设置工作,但可以让您更精细地控制调用 $em->flush()
时保留的内容。但我认为您不需要走这么远。
看到您用来更新 Fiddle 实体的代码,我认为您可以在那里改进一些东西。
首先将管理关联的责任移回实体:
/**
* @Entity
* @ChangeTrackingPolicy("DEFERRED_EXPLICIT")
*/
class Fiddle
{
// ...
/**
* @return FiddleTag[]
*/
public function getTags()
{
return $this->tags->toArray();
}
/**
* @param FiddleTag $tag
*/
public function addTag(FiddleTag $tag)
{
if (!$this->tags->contains($tag)) {
$this->tags->add($tag);
$tag->setFiddle($this);
}
}
/**
* @param FiddleTag $tag
*/
public function removeTag(FiddleTag $tag)
{
if ($this->tags->contains($tag)) {
$this->tags->removeElement($tag);
$tag->setFiddle(null);
}
}
/**
* @param FiddleTag[] $newTags
*/
public function replaceTags(array $newTags)
{
$currentTags = $this->getTags();
// remove tags that are not in the new list of tags
foreach ($currentTags as $currentTag) {
if (!in_array($currentTag, $newTags, true)) {
$this->removeTag($currentTag);
}
}
// add tags that are not in the current list of tags
foreach ($newTags as $newTag) {
if (!in_array($newTag, $currentTags, true)) {
$this->addTag($newTag);
}
}
}
// ...
}
现在您的 ImportCommand 中的代码看起来像这样:
$jsonTags = $this->getFromArray($json, 'tags');
$newTags = [];
foreach ($jsonTags as $jsonTag) {
$tag = $tagRepo->findOneByTag($jsonTag);
if ($tag === null) {
$tag = new FiddleTag();
$tag->setTag($jsonTag);
}
$newTags[] = $tag;
}
$fiddle->replaceTags($newTags);
然后当一切正常,可以持久化时,做:
$em->persist($fiddle);
foreach ($fiddle->getTags() as $tag) {
$em->persist($tag);
}
$em->flush();
在关联上配置了 cascade=persist
后,您应该能够省去手动保留标签的循环。
你可以看看 JMS Serializer图书馆和 Bundle将其集成到 Symfony 中。
关于php - 如何使用来自非托管实体的数据正确更新托管实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28031508/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!