如何在另一个实体中克隆实体对象。

huangapple go评论66阅读模式
英文:

How to clone entity object in another entity

问题

I would need to clone an entity object to another entity.

Actually, I'm using this code to clone:

$a = $em->find(Entity::class, 1);

$b = clone $a;
$em->persist($b);
$em->flush();

Everything is fine, but I need to clone that object into another entity like:

$a = $em->find(Entity::class, 1);

$b = clone $a; // in EntityBackup::class
$em->persist($b);
$em->flush();
英文:

I would need to clone an entity object to another entity.

Actually im using this code to clone:

$a = $em->find(Entity::class,1);

$b = clone $a;
$em->persist($b);
$em->flush();

Everything is fine , but i need to clone that object in another entity like:

$a = $em->find(Entity::class,1);

$b = clone $a; <----- in EntityBackup::class
$em->persist($b);
$em->flush();

答案1

得分: 1

以下是您提供的代码的中文翻译:

你可以使用序列化器/反序列化器来为EntityBackup进行数据转换,因为序列化器将你的对象转换为所需类型的数据(数组、JSON等等),当你想要反序列化时,你可以提供目标对象类:


$context = ['json_encode_options' => \JSON_UNESCAPED_UNICODE];
$serializer = new Serializer([
    new ProblemNormalizer(),
    new JsonSerializableNormalizer(),
    new ConstraintViolationListNormalizer(),
    new DateTimeNormalizer($context),
    new DateTimeZoneNormalizer(),
    new DateIntervalNormalizer(),
    new DataUriNormalizer(),
    new ArrayDenormalizer(),
    new ObjectNormalizer(null, null, null, new ReflectionExtractor()),
], [
    new JsonEncoder(),
]);

$a = $em->find(Entity::class, 1);
$aSerialized = $serializer->serialize($request, 'json', $context);

$b = $serializer->deserialize($aSerialized, EntityBackup::class, 'json', $context);

$em->persist($b);
$em->flush();

或者使用一个简单的函数来迭代每个属性:


$a = $em->find(Entity::class, 1);
$b = new EntityBackup();
$reflect = new ReflectionClass($a);
$props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
foreach ($props as $prop) {
    $getMethodName = 'set'.ucfirst($prop->getName());
    $setMethodName = 'set'.ucfirst($prop->getName());
    if (method_exists($b, $setMethodName)) {
        $b->$setMethodName($a->$getMethodName());
    }
}

$em->persist($b);
$em->flush();

并没有经过详细测试,但你可以理解思路! 如何在另一个实体中克隆实体对象。

英文:

You could hydrate you EntityBackup using a Serializer/Deserializer because the serializer transform your object into desired type of data (array, json, ...) and when you want to deserialize, you could give the destination object class:


    $context = ['json_encode_options' => \JSON_UNESCAPED_UNICODE];
    $serializer = new Serializer([
        new ProblemNormalizer(),
        new JsonSerializableNormalizer(),
        new ConstraintViolationListNormalizer(),
        new DateTimeNormalizer($context),
        new DateTimeZoneNormalizer(),
        new DateIntervalNormalizer(),
        new DataUriNormalizer(),
        new ArrayDenormalizer(),
        new ObjectNormalizer(null, null, null, new ReflectionExtractor()),
    ], [
        new JsonEncoder(),
    ]);
    
    $a = $em->find(Entity::class, 1);
    $aSerialized = $serializer->serialize($request, 'json', $context);
    
    $b = $serializer->deserialize($aSerialized, EntityBackup::class, 'json', $context);
    
    $em->persist($b);
    $em->flush();

Or use a simple function to iterate over each property:


    $a = $em->find(Entity::class, 1);
    $b = new EntityBackup();
    $reflect = new ReflectionClass($a);
    $props   = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PR);
    foreach ($props as $prop) {
        $getMethodName = 'set'.ucfirst($prop->getName());
        $setMethodName = 'set'.ucfirst($prop->getName());
        if (method_exists($b, $setMethodName)) {
            $b->$setMethodName($a->$getMethodName());
        }
    }
    
    $em->persist($b);
    $em->flush();

Not really tested but you got the idea! 如何在另一个实体中克隆实体对象。

huangapple
  • 本文由 发表于 2023年6月8日 21:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定