Skip to content

Commit

Permalink
Added , fixes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Sep 9, 2013
1 parent 5b93d89 commit ac1bd36
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 31 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ Remove one or more values from an array.
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('items', array('sword', 'shield'));

**Unset**

Remove one or more fields from a document.

DB::collection('users')->where('name', 'John')->unset('note');

You can also perform an unset on a model.

$user = User::where('name', 'John')->first();
$user->unset('note');

### Query Caching

You may easily cache the results of a query using the remember method:
Expand Down
92 changes: 61 additions & 31 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
class Builder extends \Illuminate\Database\Query\Builder {

/**
* The database collection
*
* @var MongoCollection
*/
* The database collection
*
* @var MongoCollection
*/
protected $collection;

/**
* All of the available operators.
*
* @var array
*/
* All of the available operators.
*
* @var array
*/
protected $conversion = array(
'=' => '=',
'!=' => '$ne',
Expand All @@ -29,11 +29,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
);

/**
* Create a new query builder instance.
*
* @param Connection $connection
* @return void
*/
* Create a new query builder instance.
*
* @param Connection $connection
* @return void
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
Expand Down Expand Up @@ -339,24 +339,15 @@ public function update(array $values, array $options = array())
*/
public function increment($column, $amount = 1, array $extra = array())
{
// build update statement
$update = array(
$query = array(
'$inc' => array($column => $amount),
'$set' => $extra,
);

// protect
// Protect
$this->whereNotNull($column);

// perform
$result = $this->collection->update($this->compileWheres(), $update, array('multiple' => true));

if (1 == (int) $result['ok'])
{
return $result['n'];
}

return 0;
return $this->performUpdate($query);
}

/**
Expand Down Expand Up @@ -505,6 +496,28 @@ public function pull($column, $value = null)
return $this->performUpdate($query);
}

/**
* Remove one or more fields.
*
* @param mixed $columns
* @return int
*/
public function dropColumn($columns)
{
if (!is_array($columns)) $columns = array($columns);

$fields = array();

foreach ($columns as $column)
{
$fields[$column] = 1;
}

$query = array('$unset' => $fields);

return $this->performUpdate($query);
}

/**
* Get a new instance of the query builder.
*
Expand All @@ -516,7 +529,7 @@ public function newQuery()
}

/**
* Perform update.
* Perform an update query.
*
* @param array $query
* @param array $options
Expand All @@ -541,7 +554,7 @@ protected function performUpdate($query, array $options = array())
}

/**
* Convert a key to MongoID if needed
* Convert a key to MongoID if needed.
*
* @param mixed $id
* @return mixed
Expand All @@ -557,10 +570,10 @@ protected function convertKey($id)
}

/**
* Compile the where array
*
* @return array
*/
* Compile the where array.
*
* @return array
*/
protected function compileWheres()
{
if (!$this->wheres) return array();
Expand Down Expand Up @@ -694,4 +707,21 @@ protected function compileWhereRaw($where)
return $where['sql'];
}

/**
* Handle dynamic method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if ($method == 'unset')
{
return call_user_func_array(array($this, 'dropColumn'), $parameters);
}

return parent::__call($method, $parameters);
}

}
37 changes: 37 additions & 0 deletions src/Jenssegers/Mongodb/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,41 @@ public function setRawAttributes(array $attributes, $sync = false)
parent::setRawAttributes($attributes, $sync);
}

/**
* Remove one or more fields.
*
* @param mixed $columns
* @return int
*/
public function dropColumn($columns)
{
if (!is_array($columns)) $columns = array($columns);

// Unset attributes
foreach ($columns as $column)
{
$this->__unset($column);
}

// Perform unset only on current document
return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
}

/**
* Handle dynamic method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if ($method == 'unset')
{
return call_user_func_array(array($this, 'dropColumn'), $parameters);
}

return parent::__call($method, $parameters);
}

}
27 changes: 27 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,31 @@ public function testToArray()
$this->assertEquals($original[0], $items[0]->toArray());
}

public function testUnset()
{
$user1 = User::create(array('name' => 'John Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
$user2 = User::create(array('name' => 'Jane Doe', 'note1' => 'ABC', 'note2' => 'DEF'));

$user1->unset('note1');

$this->assertFalse(isset($user1->note1));
$this->assertTrue(isset($user1->note2));
$this->assertTrue(isset($user2->note1));
$this->assertTrue(isset($user2->note2));

// Re-fetch to be sure
$user1 = User::find($user1->_id);
$user2 = User::find($user2->_id);

$this->assertFalse(isset($user1->note1));
$this->assertTrue(isset($user1->note2));
$this->assertTrue(isset($user2->note1));
$this->assertTrue(isset($user2->note2));

$user2->unset(array('note1', 'note2'));

$this->assertFalse(isset($user2->note1));
$this->assertFalse(isset($user2->note2));
}

}
22 changes: 22 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,26 @@ public function testUpsert()
$this->assertEquals(1, DB::collection('items')->count());
}

public function testUnset()
{
$id1 = DB::collection('users')->insertGetId(array('name' => 'John Doe', 'note1' => 'ABC', 'note2' => 'DEF'));
$id2 = DB::collection('users')->insertGetId(array('name' => 'Jane Doe', 'note1' => 'ABC', 'note2' => 'DEF'));

DB::collection('users')->where('name', 'John Doe')->unset('note1');

$user1 = DB::collection('users')->find($id1);
$user2 = DB::collection('users')->find($id2);

$this->assertFalse(isset($user1['note1']));
$this->assertTrue(isset($user1['note2']));
$this->assertTrue(isset($user2['note1']));
$this->assertTrue(isset($user2['note2']));

DB::collection('users')->where('name', 'Jane Doe')->unset(array('note1', 'note2'));

$user2 = DB::collection('users')->find($id2);
$this->assertFalse(isset($user2['note1']));
$this->assertFalse(isset($user2['note2']));
}

}

0 comments on commit ac1bd36

Please sign in to comment.