Skip to content

Commit 3cd44d0

Browse files
committed
Merge fixes for #418 and #439
1 parent 31774a1 commit 3cd44d0

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Illuminate\Database\Query\Builder as QueryBuilder;
1010
use Illuminate\Database\Query\Expression;
11+
use Illuminate\Support\Collection;
1112
use Jenssegers\Mongodb\Connection;
1213

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

549+
/**
550+
* Get an array with the values of a given column.
551+
*
552+
* @param string $column
553+
* @param string $key
554+
* @return array
555+
*/
556+
public function lists($column, $key = null)
557+
{
558+
if ($key == '_id')
559+
{
560+
$results = new Collection($this->get([$column, $key]));
561+
562+
// Convert MongoId's to strings so that lists can do its work.
563+
$results = $results->map(function($item)
564+
{
565+
$item['_id'] = (string) $item['_id'];
566+
567+
return $item;
568+
});
569+
570+
return $results->lists($column, $key);
571+
}
572+
573+
return parent::lists($column, $key);
574+
}
575+
548576
/**
549577
* Create a raw database expression.
550578
*
@@ -716,12 +744,12 @@ public function convertKey($id)
716744
public function where($column, $operator = null, $value = null, $boolean = 'and')
717745
{
718746
$params = func_get_args();
719-
747+
720748
// Remove the leading $ from operators.
721749
if (func_num_args() == 3)
722750
{
723751
$operator = &$params[1];
724-
752+
725753
if (starts_with($operator, '$'))
726754
{
727755
$operator = substr($operator, 1);

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function sync($ids, $detaching = true)
104104

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

107-
$detach = array_diff($current, array_keys($records));
107+
$detach = array_values(array_diff($current, array_keys($records)));
108108

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

162-
$records = $this->createAttachRecords((array) $id, $attributes);
163-
164-
// Get the ids to attach to the parent and related model.
165-
$otherIds = array_pluck($records, $this->otherKey);
166-
$foreignIds = array_pluck($records, $this->foreignKey);
162+
$ids = (array) $id;
167163

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

171-
// If we have a model instance, we can psuh the ids to that model,
167+
// If we have a model instance, we can push the ids to that model,
172168
// so that the internal attributes are updated as well. Otherwise,
173169
// we will just perform a regular database query.
174170
if (isset($model))
175171
{
176172
// Attach the new ids to the related model.
177-
$model->push($this->foreignKey, $foreignIds, true);
173+
$model->push($this->foreignKey, $this->parent->getKey(), true);
178174
}
179175
else
180176
{
181177
$query = $this->newRelatedQuery();
182178

183-
$query->where($this->related->getKeyName(), $id);
179+
// Select related models.
180+
$query->whereIn($this->related->getKeyName(), $ids);
184181

185-
// Attach the new ids to the related model.
186-
$query->push($this->foreignKey, $foreignIds, true);
182+
// Attach the new parent id to the related model.
183+
$query->push($this->foreignKey, $this->parent->getKey(), true);
187184
}
188185

189186
if ($touch) $this->touchIfTouching();

tests/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function testAuth()
102102
$port = Config::get('database.connections.mongodb.port', 27017);
103103
$database = Config::get('database.connections.mongodb.database');
104104

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

tests/RelationsTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,34 @@ public function testBelongsToManySync()
277277
$this->assertCount(1, $user->clients);
278278
}
279279

280+
public function testBelongsToManyAttachArray()
281+
{
282+
$user = User::create(array('name' => 'John Doe'));
283+
$client1 = Client::create(array('name' => 'Test 1'))->_id;
284+
$client2 = Client::create(array('name' => 'Test 2'))->_id;
285+
286+
$user = User::where('name', '=', 'John Doe')->first();
287+
$user->clients()->attach([$client1, $client2]);
288+
$this->assertCount(2, $user->clients);
289+
}
290+
291+
public function testBelongsToManySyncAlreadyPresent()
292+
{
293+
$user = User::create(array('name' => 'John Doe'));
294+
$client1 = Client::create(array('name' => 'Test 1'))->_id;
295+
$client2 = Client::create(array('name' => 'Test 2'))->_id;
296+
297+
$user->clients()->sync([$client1, $client2]);
298+
$this->assertCount(2, $user->clients);
299+
300+
$user = User::where('name', '=', 'John Doe')->first();
301+
$user->clients()->sync([$client1]);
302+
$this->assertCount(1, $user->clients);
303+
304+
$user = User::where('name', '=', 'John Doe')->first()->toArray();
305+
$this->assertCount(1, $user['client_ids']);
306+
}
307+
280308
public function testBelongsToManyCustom()
281309
{
282310
$user = User::create(array('name' => 'John Doe'));

0 commit comments

Comments
 (0)