问题描述
我无法反序列化具有多个同名元素的简单对象。
实体是微不足道的。
class Product
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=255)
* @Serializedname("@description")
*/
private $description;
/**
* @ORM\OnetoMany(targetEntity="App\Entity\Property",mappedBy="product",orphanRemoval=true)
*/
private $properties;
<?PHP
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Serializedname;
/**
* @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
*/
class Property
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=32)
* @Serializedname("@name")
*/
private $name;
/**
* @ORM\Column(type="string",length=255)
* @Serializedname("#")
*/
private $value;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Product",inversedBy="properties")
* @ORM\JoinColumn(nullable=false)
*/
private $product;
产品序列化按预期工作:
$product = (new Product())
->setDescription("Wool sweater")
->addProperty((new property())->setName('color')->setValue('blue'))
->addProperty((new property())->setName('size')->setValue('small'))
;
$xml = $this->serializer->serialize($product,'xml',$context);
完全符合我的预期:
<?xml version="1.0"?>
<product description="Wool sweater">
<property name="color">blue</property>
<property name="size">small</property>
</product>
但我无法反序列化它。
/** @var Product $product2 */
$product2 = $this->serializer->deserialize($xml,Product::class,['xml_root_node_name' => 'product'] );
if ($product2->getDescription() == $product->getDescription()) {
$io->success("deserialize success!");
} else {
$io->error($xml . " deserialize did not work :-(");
}
App\Entity\Product:
attributes:
description:
serialized_name: '@description'
groups: ['xml']
properties:
serialized_name: 'property'
groups: ['xml']
App\Entity\Property:
attributes:
name:
serialized_name: '@name'
groups: ['xml']
value:
serialized_name: '#'
groups: ['xml']
我一直在尝试使用 serialized_name、Serializedname、@Serializedname 等,但感觉就像是在进行黑客攻击。
我在 https://github.com/tacman/xml-serializer-demo
上将包含此示例的存储库放在一起git clone https://github.com/tacman/xml-serializer-demo.git bug
cd bug
composer install
bin/console app:bug
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)