dongzhang2150 2015-07-13 16:19
浏览 24

Symfony2:如何从Web服务构造实体

I'm developing a web-service where the entities are retrieved from another ReST web-service.

For now I'm getting the data from a service based on Guzzle in the controller and hydrate my entities by denormalizing with the Serializer

$customer_api = $this->container->get('customer_api');

$data = $customer_api->getCustomer([
    "customerId" => $id_customer,
]);

$customer = Customer::hydrate($data);

Here's the hydrate function

public static function hydrate($data)
{
    $encoders = array(new JsonEncoder());
    $normalizers = array(new GetSetMethodNormalizer());

    $serializer = new Serializer($normalizers, $encoders);

    $entity = $serializer->denormalize($data, get_called_class());

    return $entity;
}

As I'm getting deeper in the code I'm starting to need some entities to load other entities. But as far as I've read it's pretty bad to give access to the container to the Models

How would refactor this so I could use the model constructor instead

$customer = new Customer($id_customer);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Stata 面板数据模型选择
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用