鉴于我已经通过外部服务检索了几行作为数组。在基于 Symfony2 和 Doctrine 构建的 PHP 应用程序中。
我如何继续将数据映射到模型,以便我可以使用所有现有的业务逻辑?
*注意:*我不想自己对私有(private)领域进行反射,也不想改变模型。
我尝试研究 Doctrine 的内部结构,但很多内容似乎与查询逻辑紧密结合。我知道我可以实现一个工厂方法来在我的数据集上实例化模型,但感觉我的供应商文件夹中已经有所有类等待使用。
最佳答案
在 Symfony 中,您有 Serializer Component 。
您可以将其安装为 standalone component与 Composer :
Composer 需要 symfony/序列化器
序列化对象
$person = new \App\Entity\Person();
$person->setName('foo');
$person->setAge(99);
$person->setSportsman(false);
$jsonContent = $serializer->serialize($person, 'json');
// $jsonContent contains {"name":"foo","age":99,"sportsman":false}
echo $jsonContent; // or return it in a Response
反序列化对象
$person = $serializer->deserialize($jsonContent, \App\Entity\Person::class, 'json');
关于php - 如何在 PHP 中将预取数据映射到模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49263949/