From 0992099ec875fb2620b3602454c2b59fbeba6679 Mon Sep 17 00:00:00 2001 From: tarsis Date: Sat, 26 Mar 2016 19:29:19 -0300 Subject: [PATCH] "update" example in readme --- README.md | 37 ++++++++++++++++++++++++++---- tarsys/AqlGen/AqlGen.php | 31 ++++++++++++++++++++++++- tests/tarsys/AqlGen/AqlGenTest.php | 4 ++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dd8bf88..4a4cdad 100644 --- a/README.md +++ b/README.md @@ -169,8 +169,7 @@ echo $mainQuery->get(); */ ``` -DATA MODIFICATIONS -================================ +### Data Modification ### * Insert @@ -201,10 +200,40 @@ echo $mainQuery->get(); */ ``` +* Update -* Configuration -* Dependencies +```php + +$data = array( + "name" => "Paul", + "age" => 21 +) +$query = AqlUpdate::query('u', $data, 'users'); + +echo $mainQuery->get(); + + /* Generate this string: + UPDATE {"name": "Paul", "age": 21} IN users + */ + +//with filters +$data = array( + 'status' => "inactive" + ); + + $aql = AqlGen::query('u', 'users') + ->filter('u.status == 0') + ->update($data); + +echo $mainQuery->get(); + + /* Generate this string: + FOR u IN users + FILTER u.status == 0 + UPDATE u IN users + */ +``` ### Contribution guidelines ### diff --git a/tarsys/AqlGen/AqlGen.php b/tarsys/AqlGen/AqlGen.php index 98de387..0584d8c 100644 --- a/tarsys/AqlGen/AqlGen.php +++ b/tarsys/AqlGen/AqlGen.php @@ -304,18 +304,42 @@ public function setReturn($return) return $this; } + /** + * @param $document + * @param $collection + * @return $this + */ public function insert($document, $collection) { $this->changeOperation = new AqlInsert($document, $collection); return $this; } - public function update($document, array $changedAttributes, $collection) + /** + * @param array $changedAttributes + * @param null $document + * @param null $collection + * @return $this + */ + public function update(array $changedAttributes, $document = null, $collection = null) { + if (is_null($document)) { + $document = $this->document; + } + + if (is_null($collection)) { + $collection = $this->collection; + } $this->changeOperation = new AqlUpdate($document, $changedAttributes, $collection); return $this; } + /** + * @param $document + * @param null $collection + * @param array|null $options + * @return $this + */ public function replace($document, $collection = null, array $options = null) { if (is_null($collection)) { @@ -326,6 +350,11 @@ public function replace($document, $collection = null, array $options = null) return $this; } + /** + * @param null $document + * @param null $collection + * @return $this + */ public function remove($document = null, $collection = null) { if (is_null($document)) { diff --git a/tests/tarsys/AqlGen/AqlGenTest.php b/tests/tarsys/AqlGen/AqlGenTest.php index 0ca7212..bf8f942 100644 --- a/tests/tarsys/AqlGen/AqlGenTest.php +++ b/tests/tarsys/AqlGen/AqlGenTest.php @@ -254,14 +254,14 @@ public function testUpdateOperation() $aql = AqlGen::query('u', 'users') ->filter('u.status == 0') - ->update('u', $data, 'users'); + ->update($data); $this->assertEquals("FOR u IN users\n\tFILTER u.status == 0\nUPDATE u WITH {\"status\":\"inactive\"} IN users ", $aql->get()); //with no data $data = array(); $aql = AqlGen::query('u', 'users') - ->update('u', $data, 'users'); + ->update($data, 'u', 'users'); $this->assertEquals("FOR u IN users\nUPDATE u WITH {} IN users ", $aql->get()); }