英文:
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! ![]()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论