Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persisting entities using a REST API

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?

like image 508
Nic Wortel Avatar asked Dec 04 '13 16:12

Nic Wortel


1 Answers

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

like image 134
Tobias Avatar answered Oct 13 '22 03:10

Tobias