| 1 | <?php |
||
| 19 | trait DoctrineLoaderTrait |
||
| 20 | { |
||
| 21 | use LoaderTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Construct. |
||
| 25 | * |
||
| 26 | * @param EntityRepository $entityRepository (optionnal) |
||
| 27 | */ |
||
| 28 | public function __construct(EntityRepository $entityRepository = null) |
||
| 29 | { |
||
| 30 | if ($entityRepository) { |
||
| 31 | @trigger_error('Repository constructor injection is deprecated for ORM implementation due to circular references with Doctrine events and will be removed in 2.0. Use setEntityRepository() instead.', E_USER_DEPRECATED); |
||
|
1 ignored issue
–
show
|
|||
| 32 | $this->setEntityRepository($entityRepository); |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Checks if loader is properly configured. |
||
| 38 | * |
||
| 39 | * @throws \RuntimeException if not configured |
||
| 40 | */ |
||
| 41 | private function assertIsConfigured() |
||
| 42 | { |
||
| 43 | if (!$this->entityClass || !$this->collectionClass || !$this->filterResolver) { |
||
| 44 | throw new \RuntimeException(sprintf( |
||
| 45 | '%s methods cannot be used while it has not been initialize through %s::configureMetadata().', |
||
| 46 | static::class, |
||
| 47 | static::class |
||
| 48 | )); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Hook called with every entity or collection loaded through this loader. |
||
| 54 | * |
||
| 55 | * @param CollectionnableInterface|EntityCollection $entity |
||
| 56 | * |
||
| 57 | * @return $object same entity or collection |
||
| 58 | */ |
||
| 59 | protected function onLoad($entity) |
||
| 60 | { |
||
| 61 | @trigger_error(__METHOD__.' is deprecated and will be removed in 2.0. Use full class delegate instead, see Majora\Framework\Loader\LazyLoaderInterface.', E_USER_DEPRECATED); |
||
| 62 | |||
| 63 | return $entity; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Convert given array or Collection result set to managed entity collection class. |
||
| 68 | * |
||
| 69 | * @param array|Collection $result |
||
| 70 | * |
||
| 71 | * @return EntityCollection |
||
| 72 | */ |
||
| 73 | protected function toEntityCollection($result) |
||
| 74 | { |
||
| 75 | switch (true) { |
||
| 76 | |||
| 77 | // already a collection ? |
||
| 78 | case is_object($result) && is_subclass_of($result, $this->collectionClass) : |
||
| 79 | $collection = $result; |
||
| 80 | break; |
||
| 81 | |||
| 82 | // already a collection ? |
||
| 83 | case $result instanceof Collection : |
||
| 84 | $collection = new $this->collectionClass($result->toArray()); |
||
| 85 | break; |
||
| 86 | |||
| 87 | // simple related entity ? |
||
| 88 | case is_object($result) && is_subclass_of($result, $this->entityClass) : |
||
| 89 | $collection = new $this->collectionClass(array($result)); |
||
| 90 | break; |
||
| 91 | |||
| 92 | // simple array ? |
||
| 93 | case is_array($result) : |
||
| 94 | $collection = new $this->collectionClass($result); |
||
| 95 | break; |
||
| 96 | |||
| 97 | default: |
||
| 98 | $collection = new $this->collectionClass(); |
||
| 99 | break; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $this->onLoad($collection); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Create entity query. |
||
| 107 | * Proxy to base query builder method to use to custom all queries from this loader. |
||
| 108 | * |
||
| 109 | * @param string $alias |
||
| 110 | * |
||
| 111 | * @return QueryBuilder |
||
| 112 | */ |
||
| 113 | protected function createQuery($alias = 'entity') |
||
| 114 | { |
||
| 115 | return $this->getEntityRepository() |
||
| 116 | ->createQueryBuilder($alias) |
||
| 117 | ; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * create query an filter it with given data. |
||
| 122 | * |
||
| 123 | * @param array $filters |
||
| 124 | * |
||
| 125 | * @return Query |
||
| 126 | */ |
||
| 127 | private function createFilteredQuery(array $filters) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @see LoaderInterface::retrieveAll() |
||
| 145 | */ |
||
| 146 | public function retrieveAll(array $filters = array(), $limit = null, $offset = null) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @see LoaderInterface::retrieveOne() |
||
| 168 | */ |
||
| 169 | public function retrieveOne(array $filters = array()) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @see LoaderInterface::retrieve() |
||
| 181 | */ |
||
| 182 | public function retrieve($id) |
||
| 186 | } |
||
| 187 |
If you suppress an error, we recommend checking for the error condition explicitly: