Create Base Query with custom options for elements from parent node

@param integer $parent_id
@param boolean $include_parent

@return \Doctrine\Common\Collections\ArrayCollection
322976
public function createQueryBuilderWithParentRoot($parent_id, $include_parent = true)
    {
        $query = $this->createQueryBuilder();
        return $this->alterQueryBuilderWithParentRoot($query, $parent_id, $include_parent);
    }
859403
public static function queryByParentModelId( $parentId, $parentType, $modelId ) {

		$tableName = static::tableName();

		return self::queryWithModel()->where( "$tableName.parentId=:pid AND $tableName.parentType=:ptype AND $tableName.modelId=:mid", [ ':pid' => $parentId, ':ptype' => $parentType, ':mid' => $modelId ] );
	}
419869
public function parent_id(string $type, $id, bool $ignoreUnmapped = false): ParentId
    {
        return new ParentId($type, $id, $ignoreUnmapped);
    }
518292
public function getParents($includeSelf = true)
    {
        $parents = [];

        if (null !== $this->parent) {
            $parents = $this->getParent()->getParents();
        }

        if ($includeSelf) {
            $parents[] = $this;
        }

        return $parents;
    }
820113
public static function base($include_index = true)
	{
		$url = \Config::get('base_url');

		if ($include_index and \Config::get('index_file'))
		{
			$url .= \Config::get('index_file').'/';
		}

		return $url;
	}
1083920
public function findRelatedEntries($entry_id, $parent_field_id)
    {
        try {
            $ids = Symphony::Database()->fetchCol('entry_id', sprintf("
                SELECT `entry_id`
                FROM `tbl_entries_data_%d`
                WHERE `relation_id` = %d
                AND `entry_id` IS NOT NULL
            ", $this->get('id'), $entry_id));
        } catch (Exception $e) {
            return array();
        }

        return $ids;
    }
607679
public function buildQuery(array $params, $includeCurrent = true)
    {
        if ($includeCurrent == true) {
            $params = array_replace_recursive($this->getQuery(), $params);
        }

        return $this->serialize($params);
    }
861649
public function getCountByParent( $parentId, $parentType ) {

		$modelClass	= static::$modelClass;

		return $modelClass::queryByParent( $parentId, $parentType )->count();
	}
463651
public static function getTreeByParentId($parentId, $condition = '', $params = [], $order = 'display_order asc')
    {
        $menuMode = static::find()
            ->findByParentId($parentId)
            ->andWhere($condition, $params)
            ->orderBy($order)
            ->all();
        $tree = [];
        if ($menuMode) {
            foreach ($menuMode as $itemModel) {
                $node = [
                    'menu_id' => $itemModel['menu_id'],
                    'parent_id' => $itemModel['parent_id'],
                    'tab_id' => 'tab_' . $itemModel['menu_id'],
                    'text' => $itemModel['text'],
                    'origin_text' => $itemModel['text'],
                    'title' => $itemModel['title'],
                    'url' => $itemModel['url'],
                    'view_package' => $itemModel['view_package'],
                    'expanded' => (bool) $itemModel['expanded'],
                    'closable' => (bool) $itemModel['closable'],
                    'is_folder' => (bool) $itemModel['is_folder'],
                    'is_expand' => (bool) $itemModel['expanded'],
                    'is_open_url' => (bool) $itemModel['is_open_url'],
                    'is_open_target' => (bool) $itemModel['is_open_target'],
                    'is_every_open' => (bool) $itemModel['is_every_open'],
                    'is_hide' => (bool) $itemModel['is_hide'],
                    'display_order' => $itemModel['display_order'],
                    'params' => $itemModel['params'],
                    'note' => $itemModel['note'],
                    'status' => intval($itemModel['status']),
                ];
                $children = static::getTreeByParentId($itemModel['menu_id'], $condition, $params, $order);
                if ($children) {
                    $node['leaf'] = false;
                    $node['children'] = $children;
                } else {
                    $node['leaf'] = true;
                }
                $tree[] = $node;
            }
        }

        return $tree;
    }
30031
public function search($name,$parent_id = null){
		if ($parent_id)
			return $this->applyFilter(Geo::searchNames($name, Geo::find($parent_id)));
		else
			return $this->applyFilter(Geo::searchNames($name));
	}
278150
public function filterParent()
    {
        $parent = $this->parent;
        $db = static::getDb();
        $query = (new Query)->select(['parent'])
            ->from(static::tableName())
            ->where('[[id]]=:id');
        while ($parent) {
            if ($this->id == $parent) {
                $this->addError('parent_name', Yii::t('admin', 'Loop detected.'));
                return;
            }
            $parent = $query->params([':id' => $parent])->scalar($db);
        }
    }
802721
public static function getAccessTreeByParentId($isSuper, $menuIdList, $parentId, $condition = '', $params = [], $order = 'display_order asc')
    {
        $data = static::find()
            ->active()
            ->findByParentId($parentId)
            ->andWhere($condition, $params)
            ->orderBy($order)
            ->all();
        $tree = [];
        if ($data) {
            foreach ($data as $item) {
                if ($isSuper || in_array($item['menu_id'], $menuIdList)) {
                    $node = [
                        'menu_id' => $item['menu_id'],
                        'parent_id' => $item['parent_id'],
                        'tab_id' => 'tab_' . $item['menu_id'],
                        'text' => $item['text'],
                        'origin_text' => $item['text'],
                        'title' => $item['title'],
                        'url' => $item['url'],
                        'view_package' => $item['view_package'],
                        'expanded' => (bool) $item['expanded'],
                        'is_expand' => (bool) $item['expanded'],
                        'closable' => (bool) $item['closable'],
                        'is_open_url' => (bool) $item['is_open_url'],
                        'is_open_target' => (bool) $item['is_open_target'],
                        'is_every_open' => (bool) $item['is_every_open'],
                        'is_hide' => (bool) $item['is_hide'],
                        'display_order' => $item['display_order'],
                        'params' => $item['params'],
                        'note' => $item['note'],
                        'status' => intval($item['status']),
                    ];
                    $children = static::getAccessTreeByParentId($isSuper, $menuIdList, $item['menu_id'], $condition, $params, $order);
                    if ($children) {
                        $node['leaf'] = false;
                        $node['children'] = $children;
                    } else {
                        $node['leaf'] = true;
                    }
                    $tree[] = $node;
                }
            }
        }

        return $tree;
    }
874340
public static function findByType( $parentId, $parentType, $type, $config = [] ) {

		return self::queryByType( $parentId, $parentType, $type, $config )->all();
	}
1095416
public static function queryByType( $parentId, $parentType, $type = ModelComment::TYPE_COMMENT, $config = [] ) {

		//$type	= isset( $config[ 'type' ] ) ? $config[ 'type' ] : self::TYPE_COMMENT;
		$status	= isset( $config[ 'status' ] ) ? $config[ 'status' ] : self::STATUS_APPROVED;

		return self::queryByParent( $parentId, $parentType, $config )->andWhere( [ 'type' => $type, 'status' => $status ] );
	}
630907
protected function addChild(string $id, string $parent): ?Fluent
    {
        $node = $this->descendants($this->items, $parent);

        // it might be possible parent is not defined due to ACL, in this
        // case we should simply ignore this request as child should
        // inherit parent ACL access.
        if (! isset($node)) {
            return null;
        }

        $item = $node->get('childs');
        $item[$id] = $this->toFluent($id);

        $node->childs($item);

        return $item[$id];
    }
1148801
public function get_sub_menu_items( $menu_items, $parent_id ) {
		$items = [];

		foreach ( $menu_items as $menu_item ) {
			if ( intval( $parent_id ) === intval( $menu_item->menu_item_parent ) ) {
				$items[] = [
					'title' => $menu_item->title,
					'link' => str_replace( site_url(), '', $menu_item->url ),
					'items' => self::get_sub_menu_items( $menu_items, $menu_item->ID ),
				];
			}
		}

		return $items;
	}
1320705
public static function sortRecords(array $records, $parentId = 0, $includeParent = false, $depth = null, $height = 0)
    {
        $list = [ ];
        $map  = [ ];

        $records = self::orderRecords($records, 'position');

        foreach ($records as $record)
        {
            if ($record->getParent() === 0 || $record->getParent() > 0) // include 0 and numbers, exclude null and ''
            {
                $map[$record->getId()] = $record;
                $list[]                = [ 'id' => $record->getId(), 'parentId' => $record->getParent() ];
            }
        }

        $util      = new AdjacentList2NestedSet($list);
        $nestedSet = $util->getNestedSet();

        $root = false;

        if ($parentId != 0)
        {
            if (array_key_exists($parentId,$nestedSet)) {
                $root = $nestedSet[$parentId];
            }
            else
            {
                return [];
            }
        }

        $result = [ ];

        if ($height != 0 && $root)
        {
            foreach ($nestedSet as $id => $positioning)
            {
                if ($root['left'] > $positioning['left'] && $root['right'] < $positioning['right'])
                {
                    $result[$id] = $map[$id];
                    $result[$id]->setLevel($positioning['level']);
                }
            }
        }

        if ($includeParent && $root)
        {
            $result[$parentId] = $map[$parentId];
            $result[$parentId]->setLevel($root['level']);

        }

        if ($depth != null)
        {
            $depth = $depth + $root['level'];
        }

        foreach ($nestedSet as $id => $positioning)
        {

            if ($depth === null || $positioning['level'] <= $depth)
            {
                if ($parentId == 0 || ($positioning['left'] > $root['left'] && $positioning['right'] < $root['right']))
                {
                    $result[$id] = $map[$id];
                    $result[$id]->setLevel($positioning['level']);
                }
            }
        }

        return $result;

    }