diff --git a/src/Doctrine/ODM/OrientDB/Manager.php b/src/Doctrine/ODM/OrientDB/Manager.php index 9da0dee..558928d 100644 --- a/src/Doctrine/ODM/OrientDB/Manager.php +++ b/src/Doctrine/ODM/OrientDB/Manager.php @@ -167,7 +167,7 @@ public function findRecords(Array $rids, $fetchPlan = null, $lazy = true) */ public function flush() { - throw new \Exception; + return; } /** @@ -238,7 +238,16 @@ public function merge($object) */ public function persist($object) { - throw new \Exception(); + $json = $this->toJson($object); + if($this->checkExists($object) === false){ + $response = $this->getBinding()->postDocument($json); + $rid = $response->getData(); + $object->setRid($rid); + } else { + $response = $this->getBinding()->putDocument($object->getRid(), $json); + $result = $response->getData(); + } + return $object; } /** @@ -248,7 +257,13 @@ public function persist($object) */ public function remove($object) { - throw new \Exception(); + if($this->checkExists($object) === false){ + return false; + } + $response = $this->getBinding()->deleteDocument($object->getRid()); + $result = $response->getData(); + $object->setRid(null); + return $result; } /** @@ -366,4 +381,39 @@ protected function getBinding() { return $this->binding; } + + protected function toJson($object) + { + $data = (array) $object; + foreach ($data as $key => $value) { + $newKey = preg_replace('/[^a-z]/i', null, $key); + $data[$newKey] = $value; + unset($data[$key]); + if ( $value instanceof \Doctrine\ODM\OrientDB\Proxy\Value ) { + $object = $value->__invoke(); + $data[$newKey] = $object->getRid(); + }elseif ( $value instanceof \DateTime ) { + $data[$newKey] = $value->format('Y-m-d H:i:s'); + } + } + $data['@class'] = join('', array_slice(explode('\\', get_class($object)), -1)); + if(array_key_exists('rid', $data)){ + unset($data['rid']); + } + if(array_key_exists('version', $data)){ + $data['@version'] = $data['version']; + unset($data['version']); + } + return json_encode($data); + } + + protected function checkExists($object) + { + $rid = $object->getRid(); + if(empty($rid)){ + return false; + } + return true; + } + } diff --git a/src/Doctrine/OrientDB/Binding/Adapter/CurlClientAdapterResult.php b/src/Doctrine/OrientDB/Binding/Adapter/CurlClientAdapterResult.php index f80d33c..6aa1d40 100644 --- a/src/Doctrine/OrientDB/Binding/Adapter/CurlClientAdapterResult.php +++ b/src/Doctrine/OrientDB/Binding/Adapter/CurlClientAdapterResult.php @@ -43,8 +43,14 @@ public function getData() if (!$this->isValid()) { throw new InvalidQueryException($this->response->getBody(), $this); } + $body = $this->response->getBody(); - if (false === $json = json_decode($this->response->getBody())) { + if (null === $json = json_decode($body)) { + if ($this->isValidRid($body)) { + return $body; + } elseif ($body === "") { + return true; + } throw new \RuntimeException("Invalid JSON payload"); } @@ -77,4 +83,12 @@ public function getInnerResponse() { return $this->response; } + + /** + * {@inheritdoc} + */ + protected function isValidRid($body) + { + return preg_match('/#\d+:\d+/', $body); + } } diff --git a/src/Doctrine/OrientDB/Binding/Client/Http/CurlClientResponse.php b/src/Doctrine/OrientDB/Binding/Client/Http/CurlClientResponse.php index c9b568d..f34d2ed 100644 --- a/src/Doctrine/OrientDB/Binding/Client/Http/CurlClientResponse.php +++ b/src/Doctrine/OrientDB/Binding/Client/Http/CurlClientResponse.php @@ -118,6 +118,7 @@ public function getValidStatusCodes() self::STATUS_NO_CONTENT, self::STATUS_RESET_CONTENT, self::STATUS_PARTIAL_CONTENT, + self::STATUS_CREATED ); }