Commit 92f9f77d authored by Jens Segers's avatar Jens Segers

Insert tweak and more testing

parent 50ab8bd9
...@@ -5,6 +5,8 @@ An Eloquent model that supports MongoDB, inspired by LMongo but using original E ...@@ -5,6 +5,8 @@ An Eloquent model that supports MongoDB, inspired by LMongo but using original E
*This model extends the original Eloquent model so it uses exactly the same methods. Please note that some advanced Eloquent features may not be working, but feel free to issue a pull request!* *This model extends the original Eloquent model so it uses exactly the same methods. Please note that some advanced Eloquent features may not be working, but feel free to issue a pull request!*
For more information about Eloquent, check http://laravel.com/docs/eloquent.
Installation Installation
------------ ------------
...@@ -184,29 +186,24 @@ $user = Comment::where('body', 'like', '%spam%')->get(); ...@@ -184,29 +186,24 @@ $user = Comment::where('body', 'like', '%spam%')->get();
All basic insert, update, delete and select methods should be implemented. All basic insert, update, delete and select methods should be implemented.
**Increments & decrements** **Incrementing or decrementing a value of a column**
Perform increments (default 1) on specified attributes. Perform increments or decrements (default 1) on specified attributes:
Attention: without a where-clause, every object will be modified.
```php ```php
User::where('name', 'John Doe')->increment('age'); User::where('name', 'John Doe')->increment('age');
User::where('name', 'Bart De Wever')->decrement('weight', 50); User::where('name', 'Bart De Wever')->decrement('weight', 50);
``` ```
The number of updated objects is returned. The number of updated objects is returned:
```php ```php
$count = User->increment('age'); $count = User->increment('age');
echo $count;
``` ```
will return the number of users where `age` is a valid field. You may also specify additional columns to update:
These functions also allow for a third attribute:
```php ```php
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something')); User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight')); User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
``` ```
\ No newline at end of file
...@@ -243,9 +243,16 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -243,9 +243,16 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function insert(array $values) public function insert(array $values)
{ {
$result = $this->collection->insert($values); // Since every insert gets treated like a batch insert, we will make sure the
// bindings are structured in a way that is convenient for building these
// inserts statements by verifying the elements are actually an array.
if ( ! is_array(reset($values)))
{
$values = array($values);
}
return (1 == (int) $result['ok']); // Batch insert
return $this->collection->batchInsert($values);
} }
/** /**
......
...@@ -162,7 +162,10 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -162,7 +162,10 @@ class QueryTest extends PHPUnit_Framework_TestCase {
{ {
$users = User::whereNull('age')->get(); $users = User::whereNull('age')->get();
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
}
public function testWhereNotNull()
{
$users = User::whereNotNull('age')->get(); $users = User::whereNotNull('age')->get();
$this->assertEquals(8, count($users)); $this->assertEquals(8, count($users));
} }
...@@ -279,4 +282,46 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -279,4 +282,46 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(5, count($users)); $this->assertEquals(5, count($users));
} }
public function testUpdate()
{
User::where('name', 'John Doe')
->update(array('age' => 100));
$user = User::where('name', 'John Doe')->first();
$this->assertEquals(100, $user->age);
}
public function testDelete()
{
User::where('age', '>', 30)->delete();
$this->assertEquals(3, User::count());
}
public function testInsert()
{
User::insert(
array('name' => 'Francois', 'age' => 59, 'title' => 'Senior')
);
$this->assertEquals(10, User::count());
User::insert(array(
array('name' => 'Gaston', 'age' => 60, 'title' => 'Senior'),
array('name' => 'Jaques', 'age' => 61, 'title' => 'Senior')
));
$this->assertEquals(12, User::count());
}
public function testInsertGetId()
{
$id = User::insertGetId(
array('name' => 'Gaston', 'age' => 60, 'title' => 'Senior')
);
$this->assertEquals(10, User::count());
$this->assertNotNull($id);
$this->assertTrue(is_string($id));
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment