-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature -- experimental support of HasMany and BelongsTo relations
- Loading branch information
phoenix
committed
Jan 14, 2021
1 parent
717d29a
commit c8464b9
Showing
14 changed files
with
311 additions
and
34 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<?php | ||
|
||
Route::get('/{resource}/{resourceId}/attached/{relationship}', 'PhoenixLib\NovaNestedTreeAttachMany\Http\Controllers\NestedTreeController@attached'); | ||
Route::get('/{resource}/{resourceId}/attached/{relationship}/{idKey}', 'PhoenixLib\NovaNestedTreeAttachMany\Http\Controllers\NestedTreeController@attached'); |
This file contains 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,38 @@ | ||
<?php | ||
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers; | ||
|
||
use DomainException; | ||
|
||
class BelongsToHandler implements RelationHandler | ||
{ | ||
public function relation(): string | ||
{ | ||
return 'Illuminate\Database\Eloquent\Relations\BelongsTo'; | ||
} | ||
|
||
public function attach( $model, $relationship, $value): void | ||
{ | ||
if(is_array($value)) | ||
{ | ||
throw new DomainException('Can`t use BelongsTo relation with multiple select.'); | ||
} | ||
|
||
if(intval($value) > 0) | ||
{ | ||
$relationModel = $model->{$relationship}()->getModel(); | ||
|
||
$model->{$relationship}()->associate($relationModel->find($value)); | ||
} | ||
else | ||
{ | ||
$model->{$relationship}()->dissociate(); | ||
} | ||
|
||
$model->saveQuietly(); | ||
} | ||
|
||
public function retrieve($model, $relationship, $idKey) | ||
{ | ||
return $model->{$relationship} ? $model->{$relationship}->{$idKey}: null; | ||
} | ||
} |
This file contains 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,20 @@ | ||
<?php | ||
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers; | ||
|
||
class BelongsToManyHandler implements RelationHandler | ||
{ | ||
public function relation(): string | ||
{ | ||
return 'Illuminate\Database\Eloquent\Relations\BelongsToMany'; | ||
} | ||
|
||
public function attach( $model, $relationship, $values): void | ||
{ | ||
$model->{$relationship}()->sync($values); | ||
} | ||
|
||
public function retrieve($model, $relationship, $idKey) | ||
{ | ||
return $model->{$relationship}->pluck($idKey)->all(); | ||
} | ||
} |
This file contains 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,69 @@ | ||
<?php | ||
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers; | ||
|
||
class HasManyHandler implements RelationHandler | ||
{ | ||
private $updated = false; | ||
|
||
public function relation(): string | ||
{ | ||
return 'Illuminate\Database\Eloquent\Relations\HasMany'; | ||
} | ||
|
||
public function attach( $model, $relationship, $values): void | ||
{ | ||
$dispatcher = $model->getEventDispatcher(); | ||
|
||
$model->unsetEventDispatcher(); | ||
|
||
|
||
|
||
$relationModel = $model->{$relationship}()->getModel(); | ||
|
||
$models = $relationModel->find($values); | ||
|
||
$models->each(function ( $children ) use ( $model ){ | ||
|
||
if(!$children->is($model)) | ||
{ | ||
if(!$model->ancestors->contains($children)) | ||
{ | ||
$this->updated = true; | ||
|
||
$children->parent()->associate($model); | ||
$children->save(); | ||
} | ||
} | ||
|
||
}); | ||
|
||
|
||
|
||
foreach($model->{$relationship} as $children ) | ||
{ | ||
if(!$models->contains($children)) | ||
{ | ||
$this->updated = true; | ||
|
||
$children->parent()->dissociate(); | ||
$children->save(); | ||
} | ||
} | ||
|
||
|
||
|
||
if($this->updated) | ||
{ | ||
$model->fixTree(); | ||
} | ||
|
||
|
||
|
||
$model->setEventDispatcher($dispatcher); | ||
} | ||
|
||
public function retrieve($model, $relationship, $idKey) | ||
{ | ||
return $model->{$relationship}->pluck($idKey)->all(); | ||
} | ||
} |
This file contains 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,11 @@ | ||
<?php | ||
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers; | ||
|
||
interface RelationHandler | ||
{ | ||
public function relation(): string; | ||
|
||
public function attach( $model, $relationship, $values): void; | ||
|
||
public function retrieve($model, $relationship, $idKey); | ||
} |
This file contains 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,19 @@ | ||
<?php | ||
|
||
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation; | ||
|
||
use Illuminate\Support\Collection; | ||
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers\RelationHandler; | ||
|
||
interface RelationHandlerFactory | ||
{ | ||
public function make($relation): RelationHandler; | ||
|
||
public function register( RelationHandler $handler ): void; | ||
|
||
public function unregister( RelationHandler $handler ): void; | ||
|
||
public function registeredHandlers(): Collection; | ||
|
||
public function unregisterAll(): void; | ||
} |
This file contains 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,49 @@ | ||
<?php | ||
|
||
namespace PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation; | ||
|
||
use DomainException; | ||
use Illuminate\Support\Collection; | ||
use PhoenixLib\NovaNestedTreeAttachMany\Domain\Relation\Handlers\RelationHandler; | ||
|
||
class RelationHandlerResolver implements RelationHandlerFactory | ||
{ | ||
private $handlers; | ||
|
||
public function __construct(){ | ||
$this->handlers = new Collection; | ||
} | ||
|
||
public function make( $relation ): RelationHandler | ||
{ | ||
if($this->handlers->has( $relation )) | ||
{ | ||
return $this->handlers->get( $relation ); | ||
} | ||
|
||
throw new DomainException(sprintf('RelationHandler for relation: %s is not registered', $relation)); | ||
} | ||
|
||
public function register( RelationHandler $handler ): void | ||
{ | ||
$this->handlers->put($handler->relation(), $handler); | ||
} | ||
|
||
public function unregister( RelationHandler $handler ): void | ||
{ | ||
if($this->handlers->has($handler->relation())) | ||
{ | ||
$this->handlers->forget($handler->relation()); | ||
} | ||
} | ||
|
||
public function registeredHandlers(): Collection | ||
{ | ||
return $this->handlers; | ||
} | ||
|
||
public function unregisterAll(): void | ||
{ | ||
$this->handlers = new Collection; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.