Skip to content

Commit

Permalink
Merge pull request #1454 from RomM1/queueable-relationships
Browse files Browse the repository at this point in the history
fix serialize embedded relationships
  • Loading branch information
jenssegers authored Apr 25, 2018
2 parents 657da13 + fa045ae commit e1c6e53
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Queue\QueueableCollection;

abstract class Model extends BaseModel
{
Expand Down Expand Up @@ -420,6 +422,52 @@ protected function removeTableFromKey($key)
return $key;
}

/**
* Get the queueable relationships for the entity.
*
* @return array
*/
public function getQueueableRelations()
{
$relations = [];

foreach ($this->getRelationsWithoutParent() as $key => $relation) {
if (method_exists($this, $key)) {
$relations[] = $key;
}

if ($relation instanceof QueueableCollection) {
foreach ($relation->getQueueableRelations() as $collectionValue) {
$relations[] = $key.'.'.$collectionValue;
}
}

if ($relation instanceof QueueableEntity) {
foreach ($relation->getQueueableRelations() as $entityKey => $entityValue) {
$relations[] = $key.'.'.$entityValue;
}
}
}

return array_unique($relations);
}

/**
* Get loaded relations for the instance without parent.
*
* @return array
*/
protected function getRelationsWithoutParent()
{
$relations = $this->getRelations();

if ($parentRelation = $this->getParentRelation()) {
unset($relations[$parentRelation->getQualifiedForeignKeyName()]);
}

return $relations;
}

/**
* @inheritdoc
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,14 @@ public static function getUpdateValues($array, $prepend = '')

return $results;
}

/**
* Get the foreign key for the relationship.
*
* @return string
*/
public function getQualifiedForeignKeyName()
{
return $this->foreignKey;
}
}
19 changes: 19 additions & 0 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,23 @@ public function testPaginateEmbedsMany()
$this->assertEquals(2, $results->count());
$this->assertEquals(3, $results->total());
}

public function testGetQueueableRelationsEmbedsMany()
{
$user = User::create(['name' => 'John Doe']);
$user->addresses()->save(new Address(['city' => 'New York']));
$user->addresses()->save(new Address(['city' => 'Paris']));

$this->assertEquals(['addresses'], $user->getQueueableRelations());
$this->assertEquals([], $user->addresses->getQueueableRelations());
}

public function testGetQueueableRelationsEmbedsOne()
{
$user = User::create(['name' => 'John Doe']);
$user->father()->save(new User(['name' => 'Mark Doe']));

$this->assertEquals(['father'], $user->getQueueableRelations());
$this->assertEquals([], $user->father->getQueueableRelations());
}
}

0 comments on commit e1c6e53

Please sign in to comment.