Skip to content

Commit d3b4c9e

Browse files
authored
Merge pull request #237 from franzose/6.x
The "parent" relationship and minor tweaks
2 parents 53a5d0c + c4f53b9 commit d3b4c9e

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

docker-compose.yaml.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
POSTGRES_USER: user
2424
POSTGRES_PASSWORD: userpass
2525
php:
26-
image: php:5.6-cli
26+
image: php:7.0-cli
2727
tty: true
2828
command: /bin/sh
2929
volumes:

src/Models/Entity.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Model as Eloquent;
66
use Franzose\ClosureTable\Contracts\EntityInterface;
77
use Franzose\ClosureTable\Extensions\Collection;
8+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89
use Illuminate\Database\Eloquent\Relations\HasMany;
910
use Illuminate\Support\Arr;
1011
use InvalidArgumentException;
@@ -166,7 +167,7 @@ public function setParentIdAttribute($value)
166167
}
167168

168169
$parentId = $this->getParentIdColumn();
169-
$this->previousParentId = isset($this->original[$parentId]) ? $this->original[$parentId] : null;
170+
$this->previousParentId = $this->original[$parentId] ?? null;
170171
$this->attributes[$parentId] = $value;
171172
}
172173

@@ -212,7 +213,7 @@ public function setPositionAttribute($value)
212213
}
213214

214215
$position = $this->getPositionColumn();
215-
$this->previousPosition = isset($this->original[$position]) ? $this->original[$position] : null;
216+
$this->previousPosition = $this->original[$position] ?? null;
216217
$this->attributes[$position] = max(0, (int) $value);
217218
}
218219

@@ -298,7 +299,7 @@ public static function boot()
298299
$entity->previousPosition = null;
299300

300301
$descendant = $entity->getKey();
301-
$ancestor = isset($entity->parent_id) ? $entity->parent_id : $descendant;
302+
$ancestor = $entity->parent_id ?? $descendant;
302303

303304
$entity->closure->insertNode($ancestor, $descendant);
304305
});
@@ -354,6 +355,16 @@ public function getParent(array $columns = ['*'])
354355
return $this->exists ? $this->find($this->parent_id, $columns) : null;
355356
}
356357

358+
/**
359+
* Returns many-to-one relationship to the direct ancestor.
360+
*
361+
* @return BelongsTo
362+
*/
363+
public function parent()
364+
{
365+
return $this->belongsTo(get_class($this), $this->getParentIdColumn());
366+
}
367+
357368
/**
358369
* Returns query builder for ancestors.
359370
*
@@ -883,7 +894,7 @@ public function getChildrenRange($from, $to = null, array $columns = ['*'])
883894
public function addChild(EntityInterface $child, $position = null, $returnChild = false)
884895
{
885896
if ($this->exists) {
886-
$position = $position !== null ? $position : $this->getLatestChildPosition();
897+
$position = $position ?? $this->getLatestChildPosition();
887898

888899
$child->moveTo($position, $this);
889900
}
@@ -1596,7 +1607,7 @@ private function buildSiblingQuery(Builder $builder, $id, callable $positionCall
15961607
public function addSibling(EntityInterface $sibling, $position = null, $returnSibling = false)
15971608
{
15981609
if ($this->exists) {
1599-
$position = $position === null ? static::getLatestPosition($this) : $position;
1610+
$position = $position ?? static::getLatestPosition($this);
16001611

16011612
$sibling->moveTo($position, $this->parent_id);
16021613

@@ -1623,7 +1634,7 @@ public function addSiblings(array $siblings, $from = null)
16231634
return $this;
16241635
}
16251636

1626-
$from = $from === null ? static::getLatestPosition($this) : $from;
1637+
$from = $from ?? static::getLatestPosition($this);
16271638

16281639
$this->transactional(function () use ($siblings, &$from) {
16291640
foreach ($siblings as $sibling) {
@@ -1876,7 +1887,7 @@ public function deleteSubtree($withSelf = false, $forceDelete = false)
18761887
* @param array $models
18771888
* @return \Illuminate\Database\Eloquent\Collection
18781889
*/
1879-
public function newCollection(array $models = array())
1890+
public function newCollection(array $models = [])
18801891
{
18811892
return new Collection($models);
18821893
}

0 commit comments

Comments
 (0)