-
Notifications
You must be signed in to change notification settings - Fork 1.4k
added MorphToMany with testcases #2640
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
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Laravel\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class MorphToMany extends BelongsToMany | ||
ithuis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
protected $morphType; | ||
|
||
protected $morphClass; | ||
|
||
/** | ||
* Create a new morph to many relationship instance. | ||
* | ||
* @param Builder $query | ||
* @param Model $parent | ||
* @param string $name | ||
* @param string $table | ||
* @param string $foreignPivotKey | ||
* @param string $relatedPivotKey | ||
* @param string $parentKey | ||
* @param string $relatedKey | ||
* @param string|null $relationName | ||
* @param bool $inverse | ||
* | ||
* @return void | ||
*/ | ||
public function __construct( | ||
Builder $query, | ||
Model $parent, | ||
$name, | ||
$table, | ||
$foreignPivotKey, | ||
$relatedPivotKey, | ||
$parentKey, | ||
$relatedKey, | ||
$relationName = null, | ||
protected $inverse = false, | ||
) { | ||
$this->morphType = $name . '_type'; | ||
$this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); | ||
|
||
parent::__construct( | ||
$query, | ||
$parent, | ||
$table, | ||
$foreignPivotKey, | ||
$relatedPivotKey, | ||
$parentKey, | ||
$relatedKey, | ||
$relationName, | ||
); | ||
} | ||
|
||
/** | ||
* Attach a model to the parent. | ||
* | ||
* @param mixed $id | ||
* @param array $attributes | ||
* @param bool $touch | ||
* | ||
* @return void | ||
*/ | ||
public function attach($id, array $attributes = [], $touch = true) | ||
{ | ||
if ($id instanceof Model) { | ||
$model = $id; | ||
|
||
$id = $model->getKey(); | ||
|
||
// Attach the new parent id to the related model. | ||
$model->push($this->table, [ | ||
$this->foreignPivotKey => $this->parent->getKey(), | ||
$this->morphType => $this->parent instanceof Model ? $this->parent->getMorphClass() : null, | ||
], true); | ||
} else { | ||
if ($id instanceof Collection) { | ||
$id = $id->modelKeys(); | ||
} | ||
|
||
$query = $this->newRelatedQuery(); | ||
|
||
$query->whereIn($this->related->getKeyName(), (array) $id); | ||
|
||
// Attach the new parent id to the related model. | ||
$query->push($this->table, [ | ||
$this->foreignPivotKey => $this->parent->getKey(), | ||
$this->morphType => $this->parent instanceof Model ? $this->parent->getMorphClass() : null, | ||
], true); | ||
} | ||
|
||
// Attach the new ids to the parent model. | ||
$this->parent->push($this->relatedPivotKey, (array) $id, true); | ||
|
||
if ($touch) { | ||
$this->touchIfTouching(); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function detach($ids = [], $touch = true) | ||
{ | ||
if ($ids instanceof Model) { | ||
$ids = (array) $ids->getKey(); | ||
} | ||
|
||
$query = $this->newRelatedQuery(); | ||
|
||
// If associated IDs were passed to the method we will only delete those | ||
// associations, otherwise all of the association ties will be broken. | ||
// We'll return the numbers of affected rows when we do the deletes. | ||
$ids = (array) $ids; | ||
|
||
// Detach all ids from the parent model. | ||
$this->parent->pull($this->relatedPivotKey, $ids); | ||
|
||
// Prepare the query to select all related objects. | ||
if (count($ids) > 0) { | ||
$query->whereIn($this->related->getKeyName(), $ids); | ||
} | ||
|
||
// Remove the relation to the parent. | ||
// $query->pull($this->foreignPivotKey, $this->foreignPivotKey, $this->parent->getKey()); | ||
$query->pull($this->table, [ | ||
$this->foreignPivotKey => $this->parent->getKey(), | ||
$this->morphType => $this->parent instanceof Model ? $this->parent->getMorphClass() : null, | ||
]); | ||
|
||
if ($touch) { | ||
$this->touchIfTouching(); | ||
} | ||
|
||
return count($ids); | ||
} | ||
|
||
|
||
/** | ||
* Get the foreign key "type" name. | ||
* | ||
* @return string | ||
*/ | ||
public function getMorphType() | ||
{ | ||
return $this->morphType; | ||
} | ||
|
||
/** | ||
* Get the class name of the parent model. | ||
* | ||
* @return string | ||
*/ | ||
public function getMorphClass() | ||
{ | ||
return $this->morphClass; | ||
} | ||
|
||
/** | ||
* Get the indicator for a reverse relationship. | ||
* | ||
* @return bool | ||
*/ | ||
public function getInverse() | ||
{ | ||
return $this->inverse; | ||
} | ||
|
||
/** | ||
* Set the where clause for the relation query. | ||
* | ||
* @return $this | ||
*/ | ||
protected function setWhere() | ||
{ | ||
$foreign = $this->getForeignKey(); | ||
|
||
if ($this->getInverse()) { | ||
$this->query->where($foreign, '=', $this->parent->getKey()); | ||
} else { | ||
$relatedModels = $this->parent->{$this->relatedPivotKey} ?? []; | ||
$this->query->whereIn($this->relatedKey, $relatedModels); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our CS requires that there is a space after exclamation mark. I'll revert this changes with phpcbf before merging.