【问题标题】:Doctrine: remove, then persist with Unique Values教义:删除,然后坚持唯一值
【发布时间】:2016-07-18 09:16:31
【问题描述】:

我有以下代码。 Part 的代码列需要唯一值,所以当我remove(),然后persist() 具有相同代码值的行时会抛出错误。但是,应该首先删除该行,因此只有一个值。我必须在这些命令之间运行flush() 吗?

        if ($existingPart) { # we found one
            if ($existingPart->getPrice() != $part->getPrice()) { # price changed
                $em->remove($existingPart); #soft-delete the old one
                $em->persist($part); # persist the new part
                $countUpdates++; # get number of updated entities
            }
        } else {
            $em->persist($part); # no existing part with same code, just add new part
            $count++; # get number of new commits    
        }                    
    }

    $em->flush();

【问题讨论】:

    标签: doctrine-orm symfony


    【解决方案1】:

    啊。我发现了原因:我正在使用 softdeletable。我需要做的是从列定义中删除唯一值标志,而是在类注释中插入以下内容:

    @UniqueEntity(fields={"deletedAt","code"})
    

    这将使它检查deletedAtcode 的组合是否是唯一的,而不仅仅是字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多