Advertisement
Guest User

Example Repository Impl. with Doctrine's Entity Manager

a guest
Feb 20th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.73 KB | None | 0 0
  1. <?php
  2. namespace MyApp\Infrastructure;
  3.  
  4. use Doctrine\Common\Persistence\ObjectRepository;
  5. use Doctrine\ORM\AbstractQuery;
  6. use Doctrine\ORM\EntityManager;
  7. use MyApp\Domain\FooEntities\Foo;
  8. use MyAp\Domain\FooEntities\FooRepository;
  9.  
  10. class DoctrineFooRepository implements FooRepository
  11. {
  12.     /**
  13.      * @var ObjectRepository
  14.      */
  15.     private $repository;
  16.  
  17.     /**
  18.      * @var EntityManager
  19.      */
  20.     private $entityManager;
  21.  
  22.     public function __construct(ObjectRepository $repository, EntityManager $entityManager)
  23.     {
  24.  
  25.         $this->repository = $repository;
  26.         $this->entityManager = $entityManager;
  27.     }
  28.  
  29.     /**
  30.      * Retrieves all Foo entities
  31.      *
  32.      * @return Foo[]
  33.      */
  34.     public function all()
  35.     {
  36.         return $this->repository->findAll();
  37.     }
  38.  
  39.     /**
  40.      * Finds a Foo Entity by ID if it exists
  41.      *
  42.      * @param int $FooId
  43.      * @return Foo|null
  44.      */
  45.     public function findById($FooId)
  46.     {
  47.         return $this->repository->find($FooId);
  48.     }
  49.  
  50.     /**
  51.      * Stores a Foo Entity
  52.      *
  53.      * @param Foo $Foo
  54.      */
  55.     public function update(Foo $Foo)
  56.     {
  57.         $this->entityManager->persist($Foo);
  58.         $this->entityManager->flush($Foo);
  59.     }
  60.  
  61.     /**
  62.      * Returns an associative array of Foo entity addresses with the ID as the key and address string as the value
  63.      *
  64.      * @return string[]
  65.      */
  66.     public function allAddresses()
  67.     {
  68.  
  69.         $results = $this->repository->findAll();
  70.  
  71.         $resultsByFooId = [];
  72.         foreach($results as $result) {
  73.             $address = $result->getFullAddress();
  74.             $resultsByFooId[ $result->getId() ] = $address;
  75.         }
  76.         return $resultsByFooId;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement