Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 274ee8f

Browse files
Upgraded client to support Neo REST API changes
1 parent 5f8c076 commit 274ee8f

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

‎demo.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
* Assign some attributes to the nodes and save the,
2424
*/
2525
$firstNode->message = "Hello, ";
26+
$firstNode->blah = "blah blah";
2627
$firstNode->save();
2728

29+
$firstNode->blah = NULL; // Setting to null removes the property
30+
$firstNode->save();
31+
32+
2833
$secondNode->message = "world!";
2934
$secondNode->someOtherAttribute = 'blah blah blah';
3035
$secondNode->save();
@@ -38,6 +43,10 @@
3843
*/
3944
$relationship = $firstNode->createRelationshipTo($secondNode, 'KNOWS');
4045
$relationship->message = "brave Neo4j";
46+
$relationship->blah = "blah blah";
47+
$relationship->save();
48+
49+
$relationship->blah = NULL; // Setting to NULL removed the property
4150
$relationship->save();
4251

4352
$relationship2 = $thirdNode->createRelationshipTo($secondNode, 'LOVES');

‎php-neo-rest.php

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class PropertyContainer
4545

4646
public function __set($k, $v)
4747
{
48-
// because neo cant handle NULLs
49-
if ($v===NULL) $v = '';
50-
51-
$this->_data[$k] = $v;
48+
if ($v===NULL && isset($this->_data[$k]))
49+
unset($this->_data[$k]);
50+
else
51+
$this->_data[$k] = $v;
5252
}
5353

5454
public function __get($k)
@@ -87,16 +87,23 @@ public function delete()
8787
if (!$this->_is_new)
8888
{
8989
list($response, $http_code) = HTTPUtil::deleteRequest($this->getUri());
90+
91+
if ($http_code!=204) throw new HttpException($http_code);
92+
9093
$this->_id = NULL;
9194
$this->_id_new = TRUE;
9295
}
9396
}
9497

9598
public function save()
9699
{
97-
list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $this->_data);
98-
99-
if ($http_code!=201) throw new HttpException($http_code);
100+
if ($this->_is_new) {
101+
list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $this->_data);
102+
if ($http_code!=201) throw new HttpException($http_code);
103+
} else {
104+
list($response, $http_code) = HTTPUtil::jsonPutRequest($this->getUri().'/properties', $this->_data);
105+
if ($http_code!=204) throw new HttpException($http_code);
106+
}
100107

101108
if ($this->_is_new)
102109
{
@@ -235,16 +242,21 @@ public function getOtherNode($node)
235242

236243
public function save()
237244
{
238-
$payload = array(
245+
if ($this->_is_new) {
246+
$payload = array(
239247
'to' => $this->getEndNode()->getUri(),
240248
'type' => $this->_type,
241249
'data'=>$this->_data
242250
);
243-
244-
list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $payload);
245-
246-
if ($http_code!=201) throw new HttpException($http_code);
247-
251+
252+
list($response, $http_code) = HTTPUtil::jsonPostRequest($this->getUri(), $payload);
253+
254+
if ($http_code!=201) throw new HttpException($http_code);
255+
} else {
256+
list($response, $http_code) = HTTPUtil::jsonPutRequest($this->getUri().'/properties', $this->_data);
257+
if ($http_code!=204) throw new HttpException($http_code);
258+
}
259+
248260
if ($this->_is_new)
249261
{
250262
$this->_id = end(explode("/", $response['self']));
@@ -257,16 +269,22 @@ public function delete()
257269
if (!$this->_is_new)
258270
{
259271
list($response, $http_code) = HTTPUtil::deleteRequest($this->getUri());
272+
273+
if ($http_code!=204) throw new HttpException($http_code);
274+
260275
$this->_id = NULL;
261276
$this->_id_new = TRUE;
262277
}
263278
}
264279

265280
public function getUri()
266281
{
267-
$uri = $this->getStartNode()->getUri().'/relationships';
282+
if ($this->_is_new)
283+
$uri = $this->getStartNode()->getUri().'/relationships';
284+
else
285+
$uri = $this->_neo_db->getBaseUri().'relationship/'.$this->getId();
268286

269-
if (!$this->_is_new) $uri .= '/'.$this->getId();
287+
//if (!$this->_is_new) $uri .= '/'.$this->getId();
270288

271289
return $uri;
272290
}
@@ -314,30 +332,35 @@ class HTTPUtil
314332
function request($url, $method='GET', $post_data='', $content_type='', $accept_type='')
315333
{
316334
// Uncomment for debugging
317-
//echo 'HTTP: ', $method, " : " ,$url , "\n";
335+
//echo 'HTTP: ', $method, " : " ,$url , " : ", $post_data, "\n";
318336

319337
$ch = curl_init();
320338
curl_setopt($ch, CURLOPT_URL, $url);
321339
curl_setopt($ch, CURLOPT_HEADER, 0);
322340
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
323341
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
342+
343+
344+
//if ($method==self::POST){
345+
// curl_setopt($ch, CURLOPT_POST, true);
346+
//} else {
347+
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
348+
//}
324349

325-
if ($method==self::POST){
326-
curl_setopt($ch, CURLOPT_POST, true);
327-
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
328-
} else {
329-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
330-
}
331-
350+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
351+
332352
if ($post_data)
333353
{
354+
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
355+
334356
$headers = array(
335357
'Content-Length: ' . strlen($post_data),
336358
'Content-Type: '.$content_type,
337359
'Accept: '.$accept_type
338360
);
339361
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
340362
}
363+
341364
$response = curl_exec($ch);
342365

343366
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@@ -358,6 +381,11 @@ function jsonRequest($url, $method, $data=NULL)
358381
return $ret;
359382
}
360383

384+
function jsonPutRequest($url, $data)
385+
{
386+
return self::jsonRequest($url, self::PUT, $data);
387+
}
388+
361389
function jsonPostRequest($url, $data)
362390
{
363391
return self::jsonRequest($url, self::POST, $data);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /