Php JMS序列化程序。创建两种方式的模型&x27;s系列化,该系列化具有;“一对多”;关系
我正在使用PHP项目,偶然发现了一个问题 看看代码Php JMS序列化程序。创建两种方式的模型&x27;s系列化,该系列化具有;“一对多”;关系,php,jms-serializer,Php,Jms Serializer,我正在使用PHP项目,偶然发现了一个问题 看看代码 对不起。我发现,如果我使用组“Default”,所有的工作都是正确的 我喜欢JMS序列化程序:)耶!我将组命名为“default”,并将其更改为“default”。现在它起作用了。这是预期的行为吗?在文档中我没有看到关于组区分大小写的信息,但测试本身就说明了这一点 <?php require_once __DIR__ . '/../../../app/Autoload.php'; use JMS\Serializer\Annotati
对不起。我发现,如果我使用组“Default”,所有的工作都是正确的
我喜欢JMS序列化程序:)耶!我将组命名为“default”,并将其更改为“default”。现在它起作用了。这是预期的行为吗?在文档中我没有看到关于组区分大小写的信息,但测试本身就说明了这一点
<?php
require_once __DIR__ . '/../../../app/Autoload.php';
use JMS\Serializer\Annotation as Serializer;
/**
* @Serializer\ExclusionPolicy("all")
*/
class Order
{
/**
* @var int
* @Serializer\Type("integer")
* @Serializer\Expose
*/
private $id;
/**
* @var Product[]
* @Serializer\Type("array<Product>")
* @Serializer\Expose
*/
private $products;
/**
* @var float
* @Serializer\Type("float")
* @Serializer\Expose
*/
private $total;
private $someInternalProperty;
function __construct($products)
{
$this->id = rand(0, 100);
$this->products = $products;
$this->total = rand(100, 1000);
$this->someInternalProperty = 'Flag';
}
}
/**
* @Serializer\ExclusionPolicy("all")
*/
class Product
{
/**
* @var int
* @Serializer\Expose
* @Serializer\Type("integer")
* @Serializer\Groups({"Default", "edit"})
*/
private $id;
/**
* @var string
* @Serializer\Expose
* @Serializer\Type("string")
* @Serializer\Groups({"Default", "edit"})
*/
private $name;
/**
* @Serializer\Expose
* @Serializer\Groups({"edit"})
*/
private $price;
/**
* @Serializer\Expose
* @Serializer\Groups({"edit"})
*/
private $description;
private $hiddenProperty;
function __construct($id, $name, $price, $description)
{
$this->id = $id;
$this->name = $name;
$this->price = $price;
$this->description = $description;
$this->hiddenProperty = 42;
}
}
$product1 = new Product(
1,
'Banana',
10,
'Yellow'
);
$order = new Order([
$product1,
new Product(
2,
'Tomato',
12,
'Red'
)
]);
$serializer = \JMS\Serializer\SerializerBuilder::create()
->setPropertyNamingStrategy(new \JMS\Serializer\Naming\SerializedNameAnnotationStrategy(new \JMS\Serializer\Naming\IdenticalPropertyNamingStrategy()))
->build();
print_r([
$serializer->serialize(
$order,
'json',
\JMS\Serializer\SerializationContext::create()
->setSerializeNull(true)
->setGroups(['Default'])
),
$serializer->serialize(
$product1,
'json',
\JMS\Serializer\SerializationContext::create()
->setSerializeNull(true)
->setGroups(['edit'])
),
]);
Array
(
[0] => {"id":86,"products":[{"id":1,"name":"Banana"},{"id":2,"name":"Tomato"}],"total":644} // Here I have short model for history
[1] => {"id":1,"name":"Banana","price":10,"description":"Yellow"} // And here I have expanded model for other purpose.
)