Jarves\Storage\Propel::getBranch PHP Method

getBranch() public method

public getBranch ( $pk = null, Condition $condition = null, $depth = 1, $scope = null, $options = null )
$condition Jarves\Configuration\Condition
    public function getBranch($pk = null, Condition $condition = null, $depth = 1, $scope = null, $options = null)
    {
        $query = $this->getQueryClass();
        if (!$pk) {
            if ($scope === null && $this->definition['nestedRootAsObject']) {
                throw new \InvalidArgumentException('Argument `scope` is missing. Since this object is a nested set with different roots, we need a `scope` to get the first level.');
            }
            $parent = $query->findRoot($scope);
        } else {
            $parent = $query->findPK($this->getPropelPk($pk));
        }
        if (!$parent) {
            return null;
        }
        if ($depth === null) {
            $depth = 1;
        }
        $query = $this->getQueryClass();
        $query->childrenOf($parent);
        list($fields, $relations, $relationFields) = $this->getFields($options['fields']);
        $selects = array_keys($fields);
        $selects[] = 'Lft';
        $selects[] = 'Rgt';
        //        $selects[] = 'Title';
        $query->select($selects);
        $query->orderByBranch();
        $this->mapOptions($query, $options);
        $this->mapToOneRelationFields($query, $relations, $relationFields);
        $stmt = $this->getStm($query, $condition);
        $clazz = $this->getPhpName();
        $result = null;
        while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
            $item = $this->populateRow($clazz, $row, $selects, $relations, $relationFields, @$options['permissionCheck']);
            if ($depth > 0) {
                if (!$condition) {
                    $item['_childrenCount'] = ($item['rgt'] - $item['lft'] - 1) / 2;
                    if ($depth > 1 && $item['rgt'] - $item['lft'] > 0) {
                        $item['_children'] = $this->getBranch($this->pkFromRow($item), $condition, $depth - 1, $scope, $options);
                    }
                } else {
                    //since we have a custom (probably a permission listing condition) we have
                    //firstly to select all children and then count
                    if ($depth > 1) {
                        $item['_children'] = $this->getBranch($this->pkFromRow($item), $condition, $depth - 1, $scope, $options);
                        $item['_childrenCount'] = count($item['_children']);
                    } else {
                        $children = $this->getBranch($this->pkFromRow($item), $condition, $depth - 1, $scope, $options);
                        $item['_childrenCount'] = count($children);
                    }
                }
            }
            $result[] = $item;
        }
        return $result;
    }