Persisting entities using a REST API

2020-06-18 03:23发布

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)

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();

and this:

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

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

and this:

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

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

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

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 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?

4条回答
对你真心纯属浪费
2楼-- · 2020-06-18 04:11

I found this question while on the search for the same functionality. However, some time has passed again and it seems now that there is a solution implementing this.

From the documentation:

RAPL (RESTful API Persistence Layer) is a RESTful variant of Doctrine's ORM. It implements the same interfaces, but allows you to store and retrieve entities from a remote (RESTful) API instead of from the database.

查看更多
Juvenile、少年°
3楼-- · 2020-06-18 04:23

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. At start there is a jms serializer for deserializtion + buzz for http queries. You can wrap it with some service and works done.

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

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

查看更多
贼婆χ
4楼-- · 2020-06-18 04:24

Well Circle has built a complete REST driver for Doctrine which means you can use EXACTLY the same syntax because it IS Doctrine acting as REST client:

https://github.com/CircleOfNice/DoctrineRestDriver

查看更多
Luminary・发光体
5楼-- · 2020-06-18 04:25

you are looking for a doctrine dbal driver for rest apis i think

查看更多
登录 后发表回答