有人可以用Symfony序列化器序列化来自多个类的数据时,如何解释我如何使用多个标准化器?
可以说我有以下课程:
class User
{
private $name;
private $books;
public function __construct()
{
$this->books = new ArrayCollection();
}
// getters and setters
}
class Book
{
private $title;
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
}
Run Code Online (Sandbox Code Playgroud)
我想序列化拥有多本书的用户。
$first = new Book();
$first->setTitle('First book');
$second = new Book();
$second->setTitle('Second book');
$user = new User();
$user->setName('Person name');
$user->addBook($first);
$user->addBook($second);
dump($this->get('serializer')->serialize($user, 'json'));
die();
Run Code Online (Sandbox Code Playgroud)
假设我还想在对书籍进行序列化时包含哈希,因此我有以下规范化器:
class BookNormalizer implements NormalizerInterface
{
public function normalize($object, $format = null, …Run Code Online (Sandbox Code Playgroud)