1

I am creating an application that depends on 3rd party data from a REST API, and combines it with data from my own database.

I have a location entity endpoint at API, that I need to query using filters defined by the API (query parameters), and get a list of events at a given location from my database using Doctrine.

The issue I have is if I request an event and try to get its location, I would have to do a request to the 3rd party for each event.

I'm sure not the first one in need of combining different data sources but I haven't found much online about it.

I can cache the 3rd party data, perhaps to the database which would allow me to simply use Doctrine as usual?

Or should I create a doctrine repository thats not persisted in the database, if Doctrine allows that?

I feel like neither of those is an ideal approach. I looked into DoctrineRestDriver that I haven't tried using yet, but I don't (yet) see how would I define a different driver for a single data model (location) and if I did manage that, how would I annotate the relation?

Thanks for any input!

kachnitel
  • 450
  • 8
  • 20

1 Answers1

0

I am trying to figure this out as well, for exactly the same reasons. From what I read, there is no easy way to have easy relationships between entities that are managed by different (Doctrine) entity managers.

https://github.com/doctrine/orm/issues/5769

Using Relationships with Multiple Entity Managers

You could inject the entity manager/register into the 'event' entity and use that to get the related 'location' entity using the location_id field. Although possible, it is probably not the cleanest as you make your entities dependent on the entity manager.

https://matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/

A better solution is probably setting up a service which you inject into the 'event' entity. You then use that service to get the location.

Using EntityManager inside Doctrine 2.0 entities

Using DoctrineRestDriver, you wire up the location entity much like any other entity and you should be able to query the location (from the API) based on the location_id in the event. That logic you can easily put in a registered service which you can use in the event entity.

Although a service would solve the problem of getting the location from an event, it does not provide you with a way to run queries that join the event and location. In your event queries, you will be limited to the location_id.

I am not sure what will happen if you loop though a collection of 10 events, all with the same location. Will it make an API call 10 times to get the same location? Can this be resolved via caching to reduce the overhead?

Alternatively, you can setup a separate process that caches the API data in a local database, but that adds other complications especially related to making sure your cache is up to date with the source data. Imagine trying to keep a cache of Google Maps data up to date...

pluk77
  • 126
  • 3