/**
  * {@inheritDoc}
  */
 public function updateResource(Resource\ResourceType $resourceType, Resource\ResourceData $resourceData)
 {
     try {
         $tableGateway = $this->getTablegateway($resourceType);
         $updateData = $resourceData->getData();
         $resource = $this->getResource($resourceType, $resourceData->getResourceId());
         $data = $resource->getData();
         $mergedData = ArrayUtils::merge($data, $updateData);
         $jsonDataStr = Json::encode($mergedData);
         $affectedRows = $tableGateway->update(array('data' => $jsonDataStr), array('id' => (int) $resourceData->getResourceId()->getValue()));
         if ($affectedRows <= 0) {
             throw new RuntimeException(sprintf('Resource with id <%s> could not be updated, cause resource was not found in database.', $resourceData->getResourceId()));
         }
         $updatedResource = new Resource\ResourceData($resourceData->getResourceId());
         $updatedResource->setData($mergedData);
         return $updatedResource;
     } catch (RuntimeException $ex) {
         throw $ex;
     } catch (Exception $ex) {
         throw new RuntimeException('Updating resource failed. See previous exception for more details', null, $ex);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function updateResource(Resource\ResourceType $resourceType, Resource\ResourceData $resourceData)
 {
     try {
         $collection = $this->mongoDb->selectCollection($resourceType->getValue());
         $updateData = $resourceData->getData();
         $mongoId = new MongoId($resourceData->getResourceId()->getValue());
         $result = $collection->update(array('_id' => $mongoId), array('$set' => $updateData));
         if ($result['n'] != 1) {
             throw new RuntimeException(sprintf('Resource with id <%s> could not be updated, cause resource was not found in database.', $resourceData->getResourceId()));
         }
         return $this->getResource($resourceType, $resourceData->getResourceId());
     } catch (RuntimeException $ex) {
         throw $ex;
     } catch (Exception $ex) {
         throw new RuntimeException('Updating resource failed. See previous exception for more details', null, $ex);
     }
 }
예제 #3
0
 /**
  * @covers GingerCore\Repository\Resource\ResourceData::getResourceId
  */
 public function testGetResourceId()
 {
     $this->assertNull($this->object->getResourceId());
     $dataWithId = new ResourceData(new ResourceId('1234'));
     $this->assertEquals('1234', $dataWithId->getResourceId()->getValue());
 }