Skip to content

Support Laravel 5.5 #1295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
],
"license" : "MIT",
"require": {
"illuminate/support": "^5.1",
"illuminate/container": "^5.1",
"illuminate/database": "^5.1",
"illuminate/events": "^5.1",
"mongodb/mongodb": "^1.0.0"
"illuminate/support": "^5.5",
"illuminate/container": "^5.5",
"illuminate/database": "^5.5",
"illuminate/events": "^5.5",
"mongodb/mongodb": "^1.0.0",
"doctrine/dbal": "^2.5"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0",
Expand Down
1 change: 1 addition & 0 deletions src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Builder extends EloquentBuilder
'exists',
'push',
'pull',
'getConnection',
];

/**
Expand Down
11 changes: 8 additions & 3 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function morphTo($name = null, $type = null, $id = null)
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $parentKey = null, $relatedKey = null, $relation = null)
{
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
Expand All @@ -228,7 +228,7 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,

// Check if it is a relation with an original model.
if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $parentKey, $relatedKey, $relation);
}

// First, we'll need to determine the foreign key and "other key" for the
Expand All @@ -240,6 +240,11 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,

$otherKey = $otherKey ?: $instance->getForeignKey() . 's';

// We need to feed the primary key to the relationship
$parentKey = $this->getKeyName();

$relatedKey = $otherKey;

// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
Expand All @@ -252,7 +257,7 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();

return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $parentKey, $relatedKey, $relation);
}

/**
Expand Down
24 changes: 16 additions & 8 deletions src/Jenssegers/Mongodb/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function save(Model $model, array $joining = [], $touch = true)
/**
* @inheritdoc
*/
public function create(array $attributes, array $joining = [], $touch = true)
public function create(array $attributes = [], array $joining = [], $touch = true)
{
$instance = $this->related->newInstance($attributes);

Expand Down Expand Up @@ -184,7 +184,7 @@ public function attach($id, array $attributes = [], $touch = true)
$id = $model->getKey();

// Attach the new parent id to the related model.
$model->push($this->foreignKey, $this->parent->getKey(), true);
$model->push($this->foreignPivotKey, $this->parent->getKey(), true);
} else {
if ($id instanceof Collection) {
$id = $id->modelKeys();
Expand All @@ -195,7 +195,7 @@ public function attach($id, array $attributes = [], $touch = true)
$query->whereIn($this->related->getKeyName(), (array) $id);

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

// Attach the new ids to the parent model.
Expand Down Expand Up @@ -231,7 +231,7 @@ public function detach($ids = [], $touch = true)
}

// Remove the relation to the parent.
$query->pull($this->foreignKey, $this->parent->getKey());
$query->pull($this->foreignPivotKey, $this->parent->getKey());

if ($touch) {
$this->touchIfTouching();
Expand All @@ -245,7 +245,7 @@ public function detach($ids = [], $touch = true)
*/
protected function buildDictionary(Collection $results)
{
$foreign = $this->foreignKey;
$foreign = $this->foreignPivotKey;

// First we will build a dictionary of child models keyed by the foreign key
// of the relation so that we will easily and quickly match them to their
Expand Down Expand Up @@ -286,15 +286,23 @@ public function newRelatedQuery()
*/
public function getForeignKey()
{
return $this->foreignKey;
return $this->foreignPivotKey;
}

/**
* @inheritdoc
*/
public function getQualifiedForeignKeyName()
{
return $this->foreignKey;
return $this->foreignPivotKey;
}

/**
* @inheritdoc
*/
public function getQualifiedForeignPivotKeyName()
{
return $this->foreignPivotKey;
}

/**
Expand Down Expand Up @@ -324,6 +332,6 @@ protected function formatSyncList(array $records)
*/
public function getRelatedKey()
{
return property_exists($this, 'relatedKey') ? $this->relatedKey : $this->otherKey;
return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey;
}
}
4 changes: 2 additions & 2 deletions src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function match(array $models, Collection $results, $relation)
*
* @return Collection
*/
public function get()
public function get($columns = [])
{
return $this->getResults();
}
Expand All @@ -120,7 +120,7 @@ public function count()
public function save(Model $model)
{
$model->setParentRelation($this);

return $model->save() ? $model : false;
}

Expand Down
17 changes: 14 additions & 3 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public function testEmbedsManySave()
{
$user = User::create(['name' => 'John Doe']);
$address = new Address(['city' => 'London']);

$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));

$events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($address), $address);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address);

$events->shouldReceive('fire')->once()->with('eloquent.retrieved: ' . get_class($address), anything());
$address = $user->addresses()->save($address);

$address->unsetEventDispatcher();

$this->assertNotNull($user->addresses);
Expand All @@ -50,9 +51,12 @@ public function testEmbedsManySave()
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($address), $address);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($address), $address);
$events->shouldReceive('fire')->once()->with('eloquent.retrieved: ' . get_class($address), anything());

$address->city = 'New York';

$user->addresses()->save($address);

$address->unsetEventDispatcher();

$this->assertEquals(2, count($user->addresses));
Expand Down Expand Up @@ -213,6 +217,7 @@ public function testEmbedsManyDestroy()
$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());

$user->addresses()->destroy($address->_id);
$this->assertEquals(['Bristol', 'Bruxelles'], $user->addresses->pluck('city')->all());
Expand Down Expand Up @@ -250,7 +255,8 @@ public function testEmbedsManyDelete()

$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::type('Address'))->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
$events->shouldReceive('fire')->with('eloquent.deleted: ' . get_class($address), Mockery::type('Address'));
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());

$address->delete();

Expand Down Expand Up @@ -342,6 +348,7 @@ public function testEmbedsManyDeletingEventReturnsFalse()

$address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->once()->with('eloquent.deleting: ' . get_class($address), Mockery::mustBe($address))->andReturn(false);
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($address), anything());

$this->assertEquals(0, $user->addresses()->destroy($address));
$this->assertEquals(['New York'], $user->addresses->pluck('city')->all());
Expand Down Expand Up @@ -448,6 +455,7 @@ public function testEmbedsOne()
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.retrieved: ' . get_class($father), anything());

$father = $user->father()->save($father);
$father->unsetEventDispatcher();
Expand All @@ -467,6 +475,7 @@ public function testEmbedsOne()
$events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.updated: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.retrieved: ' . get_class($father), anything());

$father->name = 'Tom Doe';
$user->father()->save($father);
Expand All @@ -482,6 +491,7 @@ public function testEmbedsOne()
$events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($father), $father)->andReturn(true);
$events->shouldReceive('fire')->once()->with('eloquent.created: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.saved: ' . get_class($father), $father);
$events->shouldReceive('fire')->once()->with('eloquent.retrieved: ' . get_class($father), anything());

$father = $user->father()->save($father);
$father->unsetEventDispatcher();
Expand All @@ -497,6 +507,7 @@ public function testEmbedsOneAssociate()

$father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
$events->shouldReceive('until')->times(0)->with('eloquent.saving: ' . get_class($father), $father);
$events->shouldReceive('fire')->with('eloquent.retrieved: ' . get_class($father), anything());

$father = $user->father()->associate($father);
$father->unsetEventDispatcher();
Expand Down
6 changes: 3 additions & 3 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function testBelongsToMany()
// Add 2 clients
$user->clients()->save(new Client(['name' => 'Pork Pies Ltd.']));
$user->clients()->create(['name' => 'Buffet Bar Inc.']);

// Refetch
$user = User::with('clients')->find($user->_id);
$client = Client::with('users')->first();
Expand All @@ -169,10 +169,10 @@ public function testBelongsToMany()

$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
$this->assertInstanceOf('Client', $clients[0]);
$this->assertInstanceOf('User', $users[0]);
$this->assertCount(2, $user->clients);
$this->assertCount(1, $client->users);
$this->assertInstanceOf('Client', $clients[0]);
$this->assertInstanceOf('User', $users[0]);

// Now create a new user to an existing client
$user = $client->users()->create(['name' => 'Jane Doe']);
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ protected function getEnvironmentSetUp($app)
'expire' => 60,
]);
}

/**
* Enable query dumping for easier debbuging
*
* @return void
*/
protected function enableQueryDump()
{
$db = $this->app->make('db');
$db->connection('mongodb')->enableQueryLog();
$db->listen(function ($query) {
dump($query->sql);
});
}
}