/** * Return all children for the specified node. * * @param Baum\Node $node * @return Illuminate\Database\Eloquent\Collection */ public function children($node) { $query = $this->node->newQuery(); $query->where($this->node->getQualifiedParentColumnName(), '=', $node->getKey()); // We must also add the scoped column values to the query to compute valid // left and right indexes. foreach ($this->scopedAttributes($node) as $fld => $value) { $query->where($this->qualify($fld), '=', $value); } $query->orderBy($this->node->getQualifiedLeftColumnName()); $query->orderBy($this->node->getQualifiedRightColumnName()); $query->orderBy($this->node->getQualifiedKeyName()); return $query->get(); }
/** * Validates bounds of the nested tree structure. It will perform checks on * the `lft`, `rgt` and `parent_id` columns. Mainly that they're not null, * rights greater than lefts, and that they're within the bounds of the parent. * * @return boolean */ protected function validateBounds() { $connection = $this->node->getConnection(); $grammar = $connection->getQueryGrammar(); $tableName = $this->node->getTable(); $primaryKeyName = $this->node->getKeyName(); $parentColumn = $this->node->getQualifiedParentColumnName(); $lftCol = $grammar->wrap($this->node->getLeftColumnName()); $rgtCol = $grammar->wrap($this->node->getRightColumnName()); $qualifiedLftCol = $grammar->wrap($this->node->getQualifiedLeftColumnName()); $qualifiedRgtCol = $grammar->wrap($this->node->getQualifiedRightColumnName()); $qualifiedParentCol = $grammar->wrap($this->node->getQualifiedParentColumnName()); $whereStm = "({$qualifiedLftCol} IS NULL OR\n {$qualifiedRgtCol} IS NULL OR\n {$qualifiedLftCol} >= {$qualifiedRgtCol} OR\n ({$qualifiedParentCol} IS NOT NULL AND\n ({$qualifiedLftCol} <= parent.{$lftCol} OR\n {$qualifiedRgtCol} >= parent.{$rgtCol})))"; $query = $this->node->newQuery()->join($connection->raw($grammar->wrap($tableName) . ' AS parent'), $parentColumn, '=', $connection->raw('parent.' . $grammar->wrap($primaryKeyName)), 'left outer')->whereRaw($whereStm); return $query->count() == 0; }