protected function buildAncestorQuery($childId)
    {
        // We don't use Eloquent here because of endless subquery nesting
        $grammar = $this->select()->getGrammar();

        $table = $this->model->getTable();
        $idKey = $this->model->getKeyName();
        $idQueryColumn = $grammar->wrap("$table.$idKey");

        $childQuery = $this->select(["$table.$this->parentIdKey"])
                           ->where("$table.$idKey", $childId);

        $last = $childQuery;
        $queries = [];

        for ($i=1; $i <= $this->_maxDepth; $i++) {

            $queries[] = $this->buildOuterAncestorQuery($i, $last, $table, $idQueryColumn)->toSql();

            $last = $this->select([$this->parentIdKey])->whereRaw("$idQueryColumn = (" . $last->toSql() . ')');

        }

        $query = implode("\nUNION\n", $queries);

        $pdoStatement = $this->model->getConnection()->getPdo()->prepare($query);

        return new PDOPrepared($pdoStatement, $query);

    }
protected function buildAncestorQuery($childId)
    {
        // We don't use Eloquent here because of endless subquery nesting
        $grammar = $this->select()->getGrammar();

        $table = $this->model->getTable();
        $idKey = $this->model->getKeyName();
        $idQueryColumn = $grammar->wrap("$table.$idKey");

        $childQuery = $this->select(["$table.$this->parentIdKey"])
                           ->where("$table.$idKey", $childId);

        $last = $childQuery;
        $queries = [];

        for ($i=1; $i <= $this->_maxDepth; $i++) {

            $queries[] = $this->buildOuterAncestorQuery($i, $last, $table, $idQueryColumn)->toSql();

            $last = $this->select([$this->parentIdKey])->whereRaw("$idQueryColumn = (" . $last->toSql() . ')');

        }

        $query = implode("\nUNION\n", $queries);

        $pdoStatement = $this->model->getConnection()->getPdo()->prepare($query);

        return new PDOPrepared($pdoStatement, $query);

    }
protected function buildAncestorQuery ( $ childId ) { // We don't use Eloquent here because of endless subquery nesting $ grammar = $ this -> select ( ) -> getGrammar ( ) ; $ table = $ this -> model -> getTable ( ) ; $ idKey = $ this -> model -> getKeyName ( ) ; $ idQueryColumn = $ grammar -> wrap ( "$table.$idKey" ) ; $ childQuery = $ this -> select ( [ "$table.$this->parentIdKey" ] ) -> where ( "$table.$idKey" , $ childId ) ; $ last = $ childQuery ; $ queries = [ ] ; for ( $ i = 1 ; $ i <= $ this -> _maxDepth ; $ i ++ ) { $ queries [ ] = $ this -> buildOuterAncestorQuery ( $ i , $ last , $ table , $ idQueryColumn ) -> toSql ( ) ; $ last = $ this -> select ( [ $ this -> parentIdKey ] ) -> whereRaw ( "$idQueryColumn = (" . $ last -> toSql ( ) . ')' ) ; } $ query = implode ( "\nUNION\n" , $ queries ) ; $ pdoStatement = $ this -> model -> getConnection ( ) -> getPdo ( ) -> prepare ( $ query ) ; return new PDOPrepared ( $ pdoStatement , $ query ) ; }
Build the query to retrieve the parents from database.

@param int $childId

@return PDOPrepared
Build the query to retrieve the parents from database .