/** * 返回当前节点的后代 * * @param bool $self 是否包含当前节点 * @param string $direction 排序 * @param bool $directChildrenOnly 只包含相邻的下级节点 * @param bool $leavesOnly 只包含叶子节点 * @param int $limit 要获取的记录条数 * @return MPTT[] */ public function descendants($self = false, $direction = 'ASC', $directChildrenOnly = false, $leavesOnly = false, $limit = 0) { $left_operator = $self ? '>=' : '>'; $right_operator = $self ? '<=' : '<'; $query = new self(); $query->where($this->_leftColumn, $left_operator, $this->left())->where($this->_rightColumn, $right_operator, $this->right())->where($this->_scopeColumn, '=', $this->scope())->orderBy($this->_leftColumn, $direction); if ($directChildrenOnly) { if ($self) { $query->andWhereOpen()->where($this->_levelColumn, '=', $this->level())->orWhere($this->_levelColumn, '=', $this->level() + 1)->andWhereClose(); } else { $query->where($this->_levelColumn, '=', $this->level() + 1); } } if ($leavesOnly) { $query->where($this->_rightColumn, '=', $this->_leftColumn . ' + 1'); } if ($limit) { $query->limit($limit); } return $query->findAll(); }