From e8c40352c49ea63f6a1f0fa5487ff92030782f9b Mon Sep 17 00:00:00 2001 From: Jan Iwanow Date: Mon, 5 Oct 2020 20:13:58 +0700 Subject: [PATCH 1/3] bumped PHP version to 7.0 --- docker-compose.yaml.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yaml.example b/docker-compose.yaml.example index 81f84d1..8edf255 100644 --- a/docker-compose.yaml.example +++ b/docker-compose.yaml.example @@ -23,7 +23,7 @@ services: POSTGRES_USER: user POSTGRES_PASSWORD: userpass php: - image: php:5.6-cli + image: php:7.0-cli tty: true command: /bin/sh volumes: From 66a7af5f25eeb8636ad98d186732423cb5b33e14 Mon Sep 17 00:00:00 2001 From: Jan Iwanow Date: Mon, 5 Oct 2020 20:14:41 +0700 Subject: [PATCH 2/3] small tweaks for readability --- src/Models/Entity.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Models/Entity.php b/src/Models/Entity.php index df423c6..f0abb02 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -166,7 +166,7 @@ public function setParentIdAttribute($value) } $parentId = $this->getParentIdColumn(); - $this->previousParentId = isset($this->original[$parentId]) ? $this->original[$parentId] : null; + $this->previousParentId = $this->original[$parentId] ?? null; $this->attributes[$parentId] = $value; } @@ -212,7 +212,7 @@ public function setPositionAttribute($value) } $position = $this->getPositionColumn(); - $this->previousPosition = isset($this->original[$position]) ? $this->original[$position] : null; + $this->previousPosition = $this->original[$position] ?? null; $this->attributes[$position] = max(0, (int) $value); } @@ -298,7 +298,7 @@ public static function boot() $entity->previousPosition = null; $descendant = $entity->getKey(); - $ancestor = isset($entity->parent_id) ? $entity->parent_id : $descendant; + $ancestor = $entity->parent_id ?? $descendant; $entity->closure->insertNode($ancestor, $descendant); }); @@ -883,7 +883,7 @@ public function getChildrenRange($from, $to = null, array $columns = ['*']) public function addChild(EntityInterface $child, $position = null, $returnChild = false) { if ($this->exists) { - $position = $position !== null ? $position : $this->getLatestChildPosition(); + $position = $position ?? $this->getLatestChildPosition(); $child->moveTo($position, $this); } @@ -1596,7 +1596,7 @@ private function buildSiblingQuery(Builder $builder, $id, callable $positionCall public function addSibling(EntityInterface $sibling, $position = null, $returnSibling = false) { if ($this->exists) { - $position = $position === null ? static::getLatestPosition($this) : $position; + $position = $position ?? static::getLatestPosition($this); $sibling->moveTo($position, $this->parent_id); @@ -1623,7 +1623,7 @@ public function addSiblings(array $siblings, $from = null) return $this; } - $from = $from === null ? static::getLatestPosition($this) : $from; + $from = $from ?? static::getLatestPosition($this); $this->transactional(function () use ($siblings, &$from) { foreach ($siblings as $sibling) { @@ -1876,7 +1876,7 @@ public function deleteSubtree($withSelf = false, $forceDelete = false) * @param array $models * @return \Illuminate\Database\Eloquent\Collection */ - public function newCollection(array $models = array()) + public function newCollection(array $models = []) { return new Collection($models); } From c4f53b95f2924c6d41e9d70b6c32a0e1bf240a94 Mon Sep 17 00:00:00 2001 From: Jan Iwanow Date: Mon, 5 Oct 2020 20:18:31 +0700 Subject: [PATCH 3/3] introduced the "parent" relationship --- src/Models/Entity.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Models/Entity.php b/src/Models/Entity.php index f0abb02..9f4435a 100644 --- a/src/Models/Entity.php +++ b/src/Models/Entity.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model as Eloquent; use Franzose\ClosureTable\Contracts\EntityInterface; use Franzose\ClosureTable\Extensions\Collection; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Arr; use InvalidArgumentException; @@ -354,6 +355,16 @@ public function getParent(array $columns = ['*']) return $this->exists ? $this->find($this->parent_id, $columns) : null; } + /** + * Returns many-to-one relationship to the direct ancestor. + * + * @return BelongsTo + */ + public function parent() + { + return $this->belongsTo(get_class($this), $this->getParentIdColumn()); + } + /** * Returns query builder for ancestors. *