symfony - Can I use ORM without connecting entity to a database table -


i have entity pulls it's data rest web service. keep thing consistent entities in app pull data database have used orm , overridden find functions in repository.

my problem orm seems demand database table. when run doctrine:schema:update moans needing index entity when add 1 creates me table entity. guess problem in future orm want query database , not web service.

so... doing wrong?

1, if continue use orm how can stop needing database table single entity.

2, if forget orm put data loading functions? can connect entity repository without using orm?

so... doing wrong?

yes. doesn't make sense use orm interfaces if don't want use orm.

i think best approach not think implementation details @ all. introduce own interfaces repositories:

interface products {     /**      * @param string $slug      *      * @return product[]      */     public function findbyslug($slug); }  interface orders {     /**      * @param product $product      *       * @return order[]      */     public function findproductorders(product $product); } 

and implement them either orm:

class doctrineproducts implements products {     private $em;      public function __construct(entitymanager $em)     {         $this->em = $em;     }      public function findbyslug($slug)     {         return $this->em->createquerybuilder()            ->select()            // ...     } } 

or rest client:

class restorders implements orders {     private $httpclient;      public function __construct(httpclient $httpclient)     {         $this->httpclient = $httpclient;     }      public function findproductorders(product $product)     {         $orders = $this->httpclient->get(sprintf('/product/%d/orders', $product->getid()));          $orders = $this->hydrateresponsetoordersinsomeway($orders);          return $orders;     } } 

you can make methods use http client , use database in single repository.

register repositories services , use them rather calling doctrine::getrepository() directly:

services:     repository.orders:         class: doctrineorders         arguments:             - @doctrine.orm.entity_manager 

always rely on repository interfaces , never on specific implementation. in other words, use repository interface type hint:

class defaultcontroller {     private $orders;      public function __construct(orders $orders)     {         $this->orders = $orders;     }      public function indexaction(product $product)     {         $orders = $this->orders->findproductorders($product);          // ...     } } 

if don't register controllers services:

class defaultcontroller extends controller {     public function indexaction(product $product)     {         $orders = $this->get('repository.orders')->findproductorders($product);          // ...     } } 

a huge advantage of approach can change implementation details later on. mysql not enough search anymore? let's use elastic search, it's single repository!

if need call $product->getorders() , fetch orders api behind scenes should still possible of doctrine's lazy loading , event listeners.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -