Skip to content

Commit

Permalink
Merge pull request #237 from franzose/6.x
Browse files Browse the repository at this point in the history
The "parent" relationship and minor tweaks
  • Loading branch information
franzose authored Oct 5, 2020
2 parents 53a5d0c + c4f53b9 commit d3b4c9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 18 additions & 7 deletions src/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,7 +167,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;
}

Expand Down Expand Up @@ -212,7 +213,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);
}

Expand Down Expand Up @@ -298,7 +299,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);
});
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -883,7 +894,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);
}
Expand Down Expand Up @@ -1596,7 +1607,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);

Expand All @@ -1623,7 +1634,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) {
Expand Down Expand Up @@ -1876,7 +1887,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);
}
Expand Down

0 comments on commit d3b4c9e

Please sign in to comment.