Beispiel #1
0
 /**
  * Execute the request
  *
  * @param  MvcEvent $e
  * @return ReadViewModelInterface
  */
 public function onDispatch(MvcEvent $e)
 {
     $entity = $this->repository->find($this->criteria);
     if (!$entity) {
         return $this->notFoundAction();
     }
     $this->viewModel->setEntity($entity);
     $e->setResult($this->viewModel);
     return $this->viewModel;
 }
 /**
  * @param CriteriaInterface|array $criteria
  * @return EntityInterface|null
  */
 public function find($criteria)
 {
     if (is_array($criteria)) {
         $criteria = $this->createCriteria($criteria);
     }
     if (empty($this->with)) {
         return $this->entityRepository->find($criteria);
     }
     /** @var Select $select */
     $select = $criteria->getQuery();
     $select->limit(1)->offset(0);
     $result = $this->tableGateway->selectWith($select)->toArray();
     if (!isset($result[0])) {
         return;
     }
     $row = $result[0];
     $relatedEntityIds = [];
     foreach ($this->with as $relatedEntityName => $cascadeWith) {
         $relatedField = $this->getRelatedField($relatedEntityName);
         if (!array_key_exists($relatedField, $row)) {
             throw new \RuntimeException(get_class($this) . ": relation field {$relatedEntityName} not fetched");
         }
         if (!isset($relatedEntityIds[$relatedEntityName])) {
             $relatedEntityIds[$relatedEntityName] = new ArrayObject();
         }
         if (!in_array($row[$relatedField], (array) $relatedEntityIds[$relatedEntityName])) {
             $relatedEntityIds[$relatedEntityName]->append($row[$relatedField]);
         }
     }
     $relatedEntities = [];
     foreach ($this->with as $relatedEntityName => $cascadeWith) {
         if (empty((array) $relatedEntityIds[$relatedEntityName])) {
             continue;
         }
         $criteria = $this->relatedRepository[$relatedEntityName]->createCriteria(['id.in' => (array) $relatedEntityIds[$relatedEntityName]]);
         $relatedEntities[$relatedEntityName] = $this->relatedRepository[$relatedEntityName]->findMany($criteria);
     }
     $relatedField = $this->getRelatedField($relatedEntityName);
     $entityArgs = ['data' => $this->mapper->fromTableRow($row)];
     foreach ($this->relationsConfig as $entityName => $joinRule) {
         if (isset($relatedEntities[$entityName])) {
             if (isset($relatedEntities[$entityName][$row[$relatedField]])) {
                 $entityArgs['aggregateItems'][] = $relatedEntities[$entityName][$row[$relatedField]];
             } else {
                 $entityArgs['aggregateItems'][] = null;
             }
         } else {
             $entityArgs['aggregateItems'][] = null;
         }
     }
     $entity = $this->entityFactory->create($entityArgs);
     $this->with = null;
     return $entity;
 }
Beispiel #3
0
 /**
  * @return EntityInterface
  */
 public function handle($filter, $changes)
 {
     /** @var CriteriaInterface $criteria */
     $criteria = $this->repository->createCriteria($filter);
     $entity = $this->repository->find($criteria);
     if (!$entity) {
         throw new EntityNotFoundException("Entity does not found.");
     }
     if ($this->eventManager) {
         $event = $this->eventManager->createEvent('delete.pre', $entity);
         $this->eventManager->trigger($event);
     }
     $this->repository->remove($entity);
     if ($this->eventManager) {
         $event = $this->eventManager->createEvent('delete.post', $entity);
         $this->eventManager->trigger($event);
     }
     return $entity;
 }
Beispiel #4
0
 public function handle($criteria, $changes)
 {
     $criteria = $this->repository->createCriteria($criteria);
     return $this->repository->find($criteria);
 }