title: Symfony/Doctrine: Fetching entity fields from outside the database
body: <p>I'm working on an ecommerce Symfony application, but I've been really struggling to find some information about how to create entities that retrieve some data from Doctrine, and some from an external source.</p>
  
  <p>This is a website that's going to be selling the same items that are available inside of a brick-and-mortar store that already has a pre-existing inventory system.</p>
  
  <p>My current situation is that this application needs to be able to retrieve price and stock information from an external source (the REST API of our in-store inventory system), while the majority of the data is handled by Doctrine.</p>
  
  <p>For example, I have a Product class that looks like this:</p>
  
  <pre><code>class Product
  {
      protected $id;
      protected $title;
      protected $description;
  
      protected $quantity;
      protected $price
  }
  </code></pre>
  
  <p>I would like the first three fields to be handled by Doctrine like any other entity (which I already have set up, and there's tons of documentation on), but the last two to be retrieved based on an arbitrary data source (the REST API of our backend data, and this needs to be able to change as the store grows and their backend changes)</p>
  
  <p>Ultimately, I want these product entities to be callable from any standard doctrine query and have all the fields fully available like any other entity. (including price and quantity)</p>
  
  <p>I'm wondering if anybody can suggest how this problem would be solved in the Symfony ecosystem.</p>
  
  <p>At this point I'm looking at a Doctrine postLoad event subscriber that manipulates the entities before they're returned, but I'm wondering if that's the way to go, and how to structure the code to do it. (I'm guessing I first need a separate bundle that exposes a "price" service, so I can do $Service->getPrice($ProductID), then where would I place that getPrice call? I feel like it isn't something that belongs inside of the entity class itself (since that classes purpose is only to define what the entity is, not how to make one), but that's what <a href="http://docs.doctrine-project.org/en/2.0.x/reference/events.html" rel="nofollow noreferrer">Doctrine docs</a> seem to suggest)</p>
  
  <p>I've asked a similar question <a href="https://stackoverflow.com/questions/22645176/symfony2-doctrine-building-objects-from-multiple-data-sources">here</a>, but did not get very clear responses so I'm hoping that simplifying the problem might help.</p>