Skip to content

Commit

Permalink
Merge fixes for #418 and #439
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Mar 22, 2015
1 parent 31774a1 commit 3cd44d0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
32 changes: 30 additions & 2 deletions src/Jenssegers/Mongodb/Query/Builder.php
Expand Up @@ -8,6 +8,7 @@

use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Collection;
use Jenssegers\Mongodb\Connection;

class Builder extends QueryBuilder {
Expand Down Expand Up @@ -545,6 +546,33 @@ public function truncate()
return (1 == (int) $result['ok']);
}

/**
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function lists($column, $key = null)
{
if ($key == '_id')
{
$results = new Collection($this->get([$column, $key]));

// Convert MongoId's to strings so that lists can do its work.
$results = $results->map(function($item)
{
$item['_id'] = (string) $item['_id'];

return $item;
});

return $results->lists($column, $key);
}

return parent::lists($column, $key);
}

/**
* Create a raw database expression.
*
Expand Down Expand Up @@ -716,12 +744,12 @@ public function convertKey($id)
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
$params = func_get_args();

// Remove the leading $ from operators.
if (func_num_args() == 3)
{
$operator = &$params[1];

if (starts_with($operator, '$'))
{
$operator = substr($operator, 1);
Expand Down
21 changes: 9 additions & 12 deletions src/Jenssegers/Mongodb/Relations/BelongsToMany.php
Expand Up @@ -104,7 +104,7 @@ public function sync($ids, $detaching = true)

$records = $this->formatSyncList($ids);

$detach = array_diff($current, array_keys($records));
$detach = array_values(array_diff($current, array_keys($records)));

// Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the
Expand Down Expand Up @@ -159,31 +159,28 @@ public function attach($id, array $attributes = array(), $touch = true)
$model = $id; $id = $model->getKey();
}

$records = $this->createAttachRecords((array) $id, $attributes);

// Get the ids to attach to the parent and related model.
$otherIds = array_pluck($records, $this->otherKey);
$foreignIds = array_pluck($records, $this->foreignKey);
$ids = (array) $id;

// Attach the new ids to the parent model.
$this->parent->push($this->otherKey, $otherIds, true);
$this->parent->push($this->otherKey, $ids, true);

// If we have a model instance, we can psuh the ids to that model,
// If we have a model instance, we can push the ids to that model,
// so that the internal attributes are updated as well. Otherwise,
// we will just perform a regular database query.
if (isset($model))
{
// Attach the new ids to the related model.
$model->push($this->foreignKey, $foreignIds, true);
$model->push($this->foreignKey, $this->parent->getKey(), true);
}
else
{
$query = $this->newRelatedQuery();

$query->where($this->related->getKeyName(), $id);
// Select related models.
$query->whereIn($this->related->getKeyName(), $ids);

// Attach the new ids to the related model.
$query->push($this->foreignKey, $foreignIds, true);
// Attach the new parent id to the related model.
$query->push($this->foreignKey, $this->parent->getKey(), true);
}

if ($touch) $this->touchIfTouching();
Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionTest.php
Expand Up @@ -102,7 +102,7 @@ public function testAuth()
$port = Config::get('database.connections.mongodb.port', 27017);
$database = Config::get('database.connections.mongodb.database');

$this->setExpectedException('MongoConnectionException', "Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fails");
$this->setExpectedExceptionRegExp('MongoConnectionException', "/Failed to connect to: $host:$port: Authentication failed on database '$database' with username 'foo': auth fail/");
$connection = DB::connection('mongodb');
}

Expand Down
28 changes: 28 additions & 0 deletions tests/RelationsTest.php
Expand Up @@ -277,6 +277,34 @@ public function testBelongsToManySync()
$this->assertCount(1, $user->clients);
}

public function testBelongsToManyAttachArray()
{
$user = User::create(array('name' => 'John Doe'));
$client1 = Client::create(array('name' => 'Test 1'))->_id;
$client2 = Client::create(array('name' => 'Test 2'))->_id;

$user = User::where('name', '=', 'John Doe')->first();
$user->clients()->attach([$client1, $client2]);
$this->assertCount(2, $user->clients);
}

public function testBelongsToManySyncAlreadyPresent()
{
$user = User::create(array('name' => 'John Doe'));
$client1 = Client::create(array('name' => 'Test 1'))->_id;
$client2 = Client::create(array('name' => 'Test 2'))->_id;

$user->clients()->sync([$client1, $client2]);
$this->assertCount(2, $user->clients);

$user = User::where('name', '=', 'John Doe')->first();
$user->clients()->sync([$client1]);
$this->assertCount(1, $user->clients);

$user = User::where('name', '=', 'John Doe')->first()->toArray();
$this->assertCount(1, $user['client_ids']);
}

public function testBelongsToManyCustom()
{
$user = User::create(array('name' => 'John Doe'));
Expand Down

0 comments on commit 3cd44d0

Please sign in to comment.