使用 REST API 持久化实体 [英] Persisting entities using a REST API

查看:28
本文介绍了使用 REST API 持久化实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Symfony2 中的项目,我需要能够使用外部 RESTful API 而不是数据库来持久化/检索实体.由于 Doctrine 将实体映射到数据库表的一行,我认为创建从实体到外部 API 的映射也应该很容易.但是,这对我来说是新的,我似乎找不到任何关于此的描述/教程.(也许我的 Google-fu 缺少正确的词)

For a project in Symfony2 I need to be able to persist / retrieve entities using an external RESTful API, instead of the database. Since Doctrine maps the entity to a row of a database table, I thought it should be easy to create a mapping from the entity to an external API as well. However, this is new for me and I can't seem to find any descriptions / tutorials about this. (perhaps I'm missing the right words for my Google-fu)

我希望有一个类似于 Doctrine 的解决方案.我宁愿不使用基于 ActiveRecord 模式的东西,因为我希望持久性逻辑与实体分开.实体不应该知道它是如何持久化的.

I was hoping there is a solution similar to Doctrine. I'd rather not use something based on the ActiveRecord pattern, because I want the persistance logic to be seperated from the Entities. The Entity shouldn't know how it is persisted.

我希望能够做类似的事情:

I want to be able to do something similar to this:

$entity = new Entity();

$em = $this->getREST()->getManager(); // get REST Entity Manager
$em->persist($entity); // save the entity using a POST request
$em->flush();

还有这个:

$em = $this->getREST()->getManager(); // get REST Entity Manager

// retrieve the entity using a GET request
$entity = $em->getRepository('AcmeDemoBundle:Entity')->find($id);

还有这个:

$em = $this->getREST()->getManager(); // get REST Entity Manager

// retrieve all entities using a GET request
$entities = $em->getRepository('AcmeDemoBundle:Entity')->findAll();

换句话说,如果语法可以与 Doctrine 的几乎相同,那就太好了.

In other words, it would be nice if the syntax could be almost identical to Doctrine's.

此外,我想在外部文件(例如 YAML)中配置映射,而不是通过实体中的注释.(正如我所说,实体不应该知道它们是如何持久化的)

Furthermore, I'd like to configure the mapping in an external file (YAML for instance) instead of by annotations in the entity. (As I said, the entities shouldn't know how they are persisted)

Forgottenbas 已经提到了几个解决方案,但它们并不能完全满足我的要求,我希望会有更多的解决方案,因为我确信我不是第一个必须解决这个问题的人.

Forgottenbas has already mentioned a couple solutions, but they don't completely satisfy my requirements, and I'd expect there would be more solutions, as I'm sure I'm not the first one who has to tackle this problem.

有人能指出我正确的方向吗?

Can anybody point me in the right direction?

推荐答案

大约一年前,我试图找到相同问题的答案,但没有运气并创建自己的捆绑包.不幸的是,我不能分享它,因为它是专有的,不打算开源(少量设置,专门为我们的企业 API 等制作).但我可以给你一些链接

About one year ago I am trying to find the answer to same question with no luck and create own bundle. Unfortunately, I can not share it, because it's proprietary and not intended to be open-source (low amount of settings, made ​​specifically for our corporate API etc). But I can give you some links

  1. 开始时有一个用于反序列化的 jms 序列化程序 + buzz 用于 http 查询.你可以用一些服务和完成的工作来包装它.

  1. At start there is a jms serializer for deserializtion + buzz for http queries. You can wrap it with some service and works done.

Doctrine 有一些丢失的解决方案,称为 drest(doctrine rest).

Doctrine has some lost solution called drest(doctrine rest).

我还发现了一些有趣的解决方案,也称为 drest.我不尝试使用它,因为它相对较新.文档看起来不错.

Also i found some interesting solution also called drest. I dont try to use it, since it is relatively newly. Documentation looks pretty well.

这篇关于使用 REST API 持久化实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆