public function testConstructorSetsProperties() { $request = new Request('search', 'foo'); $this->assertEquals('search', $request->getOperation()); $this->assertEquals('foo', $request->getResource()); }
/** * Execute a batch create operation. * * @param Request $request * @param null|AdapterInterface $adapter Custom adapter * @return Response */ protected function executeBatchCreate(Request $request, AdapterInterface $adapter) { $t = $this->getTranslator(); if (!is_array($request->getContent())) { throw new Exception\BadRequestException($t->translate('Invalid batch operation request data.')); } // Create a simulated request for individual create events. $createRequest = new Request(Request::CREATE, $request->getResource()); // Trigger the create.pre event for every resource. foreach ($request->getContent() as $content) { $createRequest->setContent($content); $createEvent = new Event(Event::API_CREATE_PRE, $adapter, ['request' => $createRequest]); $adapter->getEventManager()->trigger($createEvent); } $response = $adapter->batchCreate($request); // Do not trigger create.post events if an error has occured or if the // response does not return valid content. if ($response->isError() || !is_array($response->getContent())) { return $response; } // Trigger the create.post event for every created resource. foreach ($response->getContent() as $resource) { $createRequest->setContent($resource); $createEvent = new Event(Event::API_CREATE_POST, $adapter, ['request' => $createRequest, 'response' => new Response($resource)]); $adapter->getEventManager()->trigger($createEvent); } return $response; }
/** * Batch create entities. * * Preserves the keys of the request content array as the keys of the * response content array. This is helpful for implementations that need to * map original identifiers to the newly created entity IDs. * * There are two outcomes if an exception is thrown during a batch. If * continueOnError is set to the request, the current entity is thrown away * but the operation continues. Otherwise, all previously created entities * are removed. * * Detaches entities after they've been created to minimize memory usage. * Because the entities are detached, this returns resource references * (containing only the entity ID) instead of full entity representations. * * {@inheritDoc} */ public function batchCreate(Request $request) { $errorStore = new ErrorStore(); $logger = $this->getServiceLocator()->get('Omeka\\Logger'); $entities = []; foreach ($request->getContent() as $key => $datum) { $entityClass = $this->getEntityClass(); $entity = new $entityClass(); $subRequest = new Request(Request::CREATE, $request->getResource()); $subRequest->setContent($datum); try { $this->hydrateEntity($subRequest, $entity, $errorStore); } catch (\Exception $e) { if ($request->continueOnError()) { $logger->err((string) $e); continue; } // Remove previously persisted entities before re-throwing. foreach ($entities as $entity) { $this->getEntityManager()->remove($entity); } $this->getEntityManager()->flush(); throw $e; } $this->getEntityManager()->persist($entity); $entities[$key] = $entity; } $this->getEntityManager()->flush(); $references = []; foreach ($entities as $key => $entity) { $references[$key] = new ResourceReference($entity, $this); $this->getEntityManager()->detach($entity); } return new Response($references); }