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 {
*/
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 {
*/
public function increment($column, $amount = 1, array $extra = array(), array $options = array())
{
$query = array(
'$inc' => array($column => $amount)
);
$query = array('$inc' => array($column => $amount));
if (!empty($extra))
if ( ! empty($extra))
{
$query['$set'] = $extra;
}
......@@ -490,7 +494,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
// Create an expression for the given value
else if (!is_null($expression))
else if ( ! is_null($expression))
{
return new Expression($expression);
}
......@@ -552,7 +556,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
public function drop($columns)
{
if (!is_array($columns)) $columns = array($columns);
if ( ! is_array($columns)) $columns = array($columns);
$fields = array();
......@@ -585,13 +589,14 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
protected function performUpdate($query, array $options = array())
{
// Default options
$default = array('multiple' => true);
// Merge options and override default options
$options = array_merge($default, $options);
// Update multiple items by default.
if ( ! array_key_exists('multiple', $options))
{
$options['multiple'] = true;
}
$wheres = $this->compileWheres();
$result = $this->collection->update($wheres, $query, $options);
if (1 == (int) $result['ok'])
......@@ -682,7 +687,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
// 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.
if (isset($where['values']))
......
......@@ -48,6 +48,12 @@ abstract class EmbedsOneOrMany extends Relation {
$this->foreignKey = $foreignKey;
$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();
}
......@@ -312,4 +318,14 @@ abstract class EmbedsOneOrMany extends Relation {
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 {
$this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
}
/*public function testEmbedsManyDestroy()
public function testEmbedsManyDestroy()
{
$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'))));
......@@ -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'))));
$user->addresses()->destroy(array($london, $bruxelles));
$this->assertEquals(array('Bristol'), $user->addresses->lists('city'));
}*/
}
public function testEmbedsManyDissociate()
{
......@@ -498,7 +498,6 @@ class EmbeddedRelationsTest extends TestCase {
$user = User::where('name', 'John Doe')->first();
$this->assertEquals('Ghent', $user->addresses->first()->city);
$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