Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
"update" example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsislima committed Mar 26, 2016
1 parent 84cbd1f commit 0992099
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ echo $mainQuery->get();
*/
```

DATA MODIFICATIONS
================================
### Data Modification ###

* Insert

Expand Down Expand Up @@ -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 ###

Expand Down
31 changes: 30 additions & 1 deletion tarsys/AqlGen/AqlGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/tarsys/AqlGen/AqlGenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 0992099

Please sign in to comment.