Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I needed these methods so I thought I'd share #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 83 additions & 27 deletions classes/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function tree_new_next_sibling_of(\Nestedsets\Model $object)
*/
public function tree_get_root()
{
$query = $this->find()->where($this->configuration['left_field'], 1);
$query = $this->query()->where($this->configuration['left_field'], 1);

if ( ! is_null($this->configuration['tree_field']))
{
Expand All @@ -362,7 +362,7 @@ public function tree_get_root()
*/
public function tree_get_query()
{
$query = $this->find()->where('id', 1);
$query = $this->query()->where('id', 1);

if ( ! is_null($this->configuration['tree_field']))
{
Expand All @@ -387,7 +387,7 @@ public function tree_get_parent(\Nestedsets\Model $object = null)

$this->tree_same_model_as($object, __METHOD__);

$query = $this->find()
$query = $this->query()
->where($this->configuration['left_field'], '<', $object->{$this->configuration['left_field']})
->where($this->configuration['right_field'], '>', $object->{$this->configuration['right_field']})
->order_by($this->configuration['right_field'], 'ASC');
Expand Down Expand Up @@ -415,7 +415,7 @@ public function tree_get_first_child(\Nestedsets\Model $object = null)

$this->tree_same_model_as($object, __METHOD__);

$query = $this->find()
$query = $this->query()
->where($this->configuration['left_field'], $object->{$this->configuration['left_field']} + 1);

if ( ! is_null($this->configuration['tree_field']))
Expand All @@ -441,7 +441,7 @@ public function tree_get_last_child(\Nestedsets\Model $object = null)

$this->tree_same_model_as($object, __METHOD__);

$query = $this->find()
$query = $this->query()
->where($this->configuration['right_field'], $object->{$this->configuration['right_field']} - 1);

if ( ! is_null($this->configuration['tree_field']))
Expand All @@ -465,23 +465,79 @@ public function tree_get_children(\Nestedsets\Model $object = null)
{
is_null($object) and $object = $this;

if ($child = $object->tree_get_first_child())
{
$result = array($child->id => $child);
while ($child = $child->tree_get_next_sibling())
{
$result[$child->id] = $child;
}
$lf = $this->configuration['left_field'];
$rf = $this->configuration['right_field'];
$left = $object->{$lf};
$right = $object->{$rf};

// return the array of Nestedsets Model objects
return $result;
}
else
$query = \DB::select('child.id')
->from([static::table(), 'child'])
->join([static::table(), 'ancestor'], 'left')
->on('ancestor.' . $lf, 'BETWEEN', \DB::expr(($left + 1) . ' AND ' . ($right - 1)))
->on('child.' . $lf, 'BETWEEN', \DB::expr('ancestor.left + 1 AND ancestor.right - 1'))
->where('child.' . $lf, 'BETWEEN', \DB::expr(($left + 1) . ' AND ' . ($right - 1)))
->and_where('ancestor.id', null);

if ( ! is_null($this->configuration['tree_field']))
{
return null;
$query->where($this->configuration['tree_field'], $this->tree_get_tree_id());
}

$ids = $query->execute()
->as_array();

if (! $ids) {
return [];
}

return static::query()
->where('id', 'in', $ids)
->get();
}


/**
* returns all descendants of the object passed.
*
* @param object Nestedsets\Model
* @return array Nestedsets\Model
*/
public function tree_get_descendants(\Nestedsets\Model $object = null)
{
is_null($object) and $object = $this;

$lf = $this->configuration['left_field'];
$rf = $this->configuration['right_field'];

$query = $this->query()
->where($lf, '>', $this->$lf)
->where($rf, '<', $this->$rf)
->get();

return $query;
}

/**
* returns all leafs of the tree passed.
*
* @param object Nestedsets\Model
* @return array Nestedsets\Model
*/
public function tree_get_leaf_descendants(\Nestedsets\Model $object = null)
{
is_null($object) and $object = $this;

$lf = $this->configuration['left_field'];
$rf = $this->configuration['right_field'];

$query = $this->query()
->where($lf, '>', $this->$lf)
->where($rf, '<', $this->$rf)
->where(\DB::expr(\DB::quote_identifier($rf) . ' - ' . \DB::quote_identifier($lf)), '=', 1)
->get();

return $query;
}
// -----------------------------------------------------------------

/**
Expand All @@ -496,7 +552,7 @@ public function tree_get_previous_sibling(\Nestedsets\Model $object = null)

$this->tree_same_model_as($object, __METHOD__);

$query = $this->find()
$query = $this->query()
->where($this->configuration['right_field'], $object->{$this->configuration['left_field']} - 1);

if ( ! is_null($this->configuration['tree_field']))
Expand All @@ -522,7 +578,7 @@ public function tree_get_next_sibling(\Nestedsets\Model $object = null)

$this->tree_same_model_as($object, __METHOD__);

$query = $this->find()
$query = $this->query()
->where($this->configuration['left_field'], $object->{$this->configuration['right_field']} + 1);

if ( ! is_null($this->configuration['tree_field']))
Expand Down Expand Up @@ -747,7 +803,7 @@ public function tree_depth(\Nestedsets\Model $object = null)

if ($this->tree_is_valid($object))
{
$query = $this->find();
$query = $this->query();

if ( ! is_null($this->configuration['tree_field']))
{
Expand Down Expand Up @@ -1212,8 +1268,8 @@ protected function _get_one($query)
}
$this->relations = array();

// return the query result
return $query->get_one();
// ORM's get_one is broken when using relations.
return current($query->get());
}

// -----------------------------------------------------------------
Expand All @@ -1230,7 +1286,7 @@ protected function _tree_dump_as($type, $attributes, $skip_root)
{
if ($this->tree_is_valid($this))
{
$query = $this->find();
$query = $this->query();

// if we have multiple roots
if ( ! is_null($this->configuration['tree_field']))
Expand Down Expand Up @@ -1428,7 +1484,7 @@ protected function _tree_shift_rlvalues($first, $delta)

// set clause
$query->set(array(
$this->configuration['left_field'] => \DB::expr($this->configuration['left_field'].$sqldelta),
$this->configuration['left_field'] => \DB::expr(\DB::quote_identifier($this->configuration['left_field']).$sqldelta),
));

// update in the correct order to avoid constraint conflicts
Expand All @@ -1450,7 +1506,7 @@ protected function _tree_shift_rlvalues($first, $delta)

// set clause
$query->set(array(
$this->configuration['right_field'] => \DB::expr($this->configuration['right_field'].$sqldelta),
$this->configuration['right_field'] => \DB::expr(\DB::quote_identifier($this->configuration['right_field']).$sqldelta),
));

// update in the correct order to avoid constraint conflicts
Expand Down Expand Up @@ -1484,8 +1540,8 @@ protected function _tree_shift_rlrange($first, $last, $delta)
// set clause
$delta = ($delta < 0) ? ('- '.abs($delta)) : ('+ '.$delta);
$query->set(array(
$this->configuration['left_field'] => \DB::expr($this->configuration['left_field'].$delta),
$this->configuration['right_field'] => \DB::expr($this->configuration['right_field'].$delta),
$this->configuration['left_field'] => \DB::expr(\DB::quote_identifier($this->configuration['left_field']).$delta),
$this->configuration['right_field'] => \DB::expr(\DB::quote_identifier($this->configuration['right_field']).$delta),
));

// update back to front
Expand Down