/** * {@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); } }
/** * @covers GingerCore\Repository\Resource\ResourceData::setData * @covers GingerCore\Repository\Resource\ResourceData::getData */ public function testSetGetData() { $this->object->setData(array('akey' => 'somevalue')); $this->assertEquals(array('akey' => 'somevalue'), $this->object->getData()); }