Commit 5e0e2882 authored by Jens Segers's avatar Jens Segers

Small tweaks

parent 357f65f5
...@@ -363,7 +363,13 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -363,7 +363,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function update(array $values, array $options = array()) public function update(array $values, array $options = array())
{ {
return $this->performUpdate(array('$set' => $values), $options); // Use $set as default operator.
if ( ! starts_with(key($values), '$'))
{
$values = array('$set' => $values);
}
return $this->performUpdate($values, $options);
} }
/** /**
...@@ -376,11 +382,9 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -376,11 +382,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function increment($column, $amount = 1, array $extra = array(), array $options = array()) public function increment($column, $amount = 1, array $extra = array(), array $options = array())
{ {
$query = array( $query = array('$inc' => array($column => $amount));
'$inc' => array($column => $amount)
);
if (!empty($extra)) if ( ! empty($extra))
{ {
$query['$set'] = $extra; $query['$set'] = $extra;
} }
...@@ -490,7 +494,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -490,7 +494,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
} }
// Create an expression for the given value // Create an expression for the given value
else if (!is_null($expression)) else if ( ! is_null($expression))
{ {
return new Expression($expression); return new Expression($expression);
} }
...@@ -552,7 +556,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -552,7 +556,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function drop($columns) public function drop($columns)
{ {
if (!is_array($columns)) $columns = array($columns); if ( ! is_array($columns)) $columns = array($columns);
$fields = array(); $fields = array();
...@@ -585,13 +589,14 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -585,13 +589,14 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
protected function performUpdate($query, array $options = array()) protected function performUpdate($query, array $options = array())
{ {
// Default options // Update multiple items by default.
$default = array('multiple' => true); if ( ! array_key_exists('multiple', $options))
{
// Merge options and override default options $options['multiple'] = true;
$options = array_merge($default, $options); }
$wheres = $this->compileWheres(); $wheres = $this->compileWheres();
$result = $this->collection->update($wheres, $query, $options); $result = $this->collection->update($wheres, $query, $options);
if (1 == (int) $result['ok']) if (1 == (int) $result['ok'])
...@@ -682,7 +687,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -682,7 +687,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
} }
// Convert id's. // Convert id's.
if (isset($where['column']) and $where['column'] == '_id') if (isset($where['column']) and ($where['column'] == '_id' or ends_with($where['column'], '._id')))
{ {
// Multiple values. // Multiple values.
if (isset($where['values'])) if (isset($where['values']))
......
...@@ -48,6 +48,12 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -48,6 +48,12 @@ abstract class EmbedsOneOrMany extends Relation {
$this->foreignKey = $foreignKey; $this->foreignKey = $foreignKey;
$this->relation = $relation; $this->relation = $relation;
// If this is a nested relation, we need to get the parent query instead.
if ($parentRelation = $this->getParentRelation())
{
$this->query = $parentRelation->getQuery();
}
$this->addConstraints(); $this->addConstraints();
} }
...@@ -312,4 +318,14 @@ abstract class EmbedsOneOrMany extends Relation { ...@@ -312,4 +318,14 @@ abstract class EmbedsOneOrMany extends Relation {
return $model; return $model;
} }
/**
* Get the relation instance of the parent.
*
* @return Illuminate\Database\Eloquent\Relations\Relation
*/
protected function getParentRelation()
{
return $this->parent->getParent();
}
} }
...@@ -179,7 +179,7 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -179,7 +179,7 @@ class EmbeddedRelationsTest extends TestCase {
$this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city')); $this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
} }
/*public function testEmbedsManyDestroy() public function testEmbedsManyDestroy()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
$user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles')))); $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
...@@ -215,7 +215,7 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -215,7 +215,7 @@ class EmbeddedRelationsTest extends TestCase {
list($london, $bristol, $bruxelles) = $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles')))); list($london, $bristol, $bruxelles) = $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
$user->addresses()->destroy(array($london, $bruxelles)); $user->addresses()->destroy(array($london, $bruxelles));
$this->assertEquals(array('Bristol'), $user->addresses->lists('city')); $this->assertEquals(array('Bristol'), $user->addresses->lists('city'));
}*/ }
public function testEmbedsManyDissociate() public function testEmbedsManyDissociate()
{ {
...@@ -498,7 +498,6 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -498,7 +498,6 @@ class EmbeddedRelationsTest extends TestCase {
$user = User::where('name', 'John Doe')->first(); $user = User::where('name', 'John Doe')->first();
$this->assertEquals('Ghent', $user->addresses->first()->city); $this->assertEquals('Ghent', $user->addresses->first()->city);
$this->assertEquals('Mark Doe', $user->father->name); $this->assertEquals('Mark Doe', $user->father->name);
} }
} }
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