framework/Orm/Extensions/Tree.php
protected function insertModelAsLastRoot(Model $model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->select('MAX(__rgt)')
            ->from($tableName);
        $maxRgt = (int)$connection->query($query)->fetchScalar();

        if ($model->isNew()) {

            $model->__lft = $maxRgt + 1;
            $model->__rgt = $model->__lft + 1;
            $model->__lvl = 0;
            $model->__prt = null;

        } else {

            $query = (new Query())
                ->update()
                ->table($tableName)
                ->values([
                    '__lft' => '__lft + :max - :lft + 1',
                    '__rgt' => '__rgt + :max - :lft + 1',
                    '__lvl' => '__lvl - :lvl',
                ])
                ->where('__lft>=:lft AND __rgt<=:rgt')
                ->params([':max' => $maxRgt, ':lft' => $model->__lft, ':rgt' => $model->__rgt, ':lvl' => $model->__lvl]);
            $connection->execute($query);

            $this->removeFromTreeByElement($model);

            // TODO: calculate new __lft, __rgt!
            $model->refreshTreeColumns();
            $model->__lvl = 0;
            $model->__prt = null;
        }
    }
protected function insertModelAsLastRoot(Model $model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->select('MAX(__rgt)')
            ->from($tableName);
        $maxRgt = (int)$connection->query($query)->fetchScalar();

        if ($model->isNew()) {

            $model->__lft = $maxRgt + 1;
            $model->__rgt = $model->__lft + 1;
            $model->__lvl = 0;
            $model->__prt = null;

        } else {

            $query = (new Query())
                ->update()
                ->table($tableName)
                ->values([
                    '__lft' => '__lft + :max - :lft + 1',
                    '__rgt' => '__rgt + :max - :lft + 1',
                    '__lvl' => '__lvl - :lvl',
                ])
                ->where('__lft>=:lft AND __rgt<=:rgt')
                ->params([':max' => $maxRgt, ':lft' => $model->__lft, ':rgt' => $model->__rgt, ':lvl' => $model->__lvl]);
            $connection->execute($query);

            $this->removeFromTreeByElement($model);

            // TODO: calculate new __lft, __rgt!
            $model->refreshTreeColumns();
            $model->__lvl = 0;
            $model->__prt = null;
        }
    }
protected function insertModelAsLastRoot ( Model $ model ) { /** @var \T4\Orm\Model $class */ $ class = get_class ( $ model ) ; $ tableName = $ class :: getTableName ( ) ; /** @var \T4\Dbal\Connection $connection */ $ connection = $ class :: getDbConnection ( ) ; $ query = ( new Query ( ) ) -> select ( 'MAX(__rgt)' ) -> from ( $ tableName ) ; $ maxRgt = ( int ) $ connection -> query ( $ query ) -> fetchScalar ( ) ; if ( $ model -> isNew ( ) ) { $ model -> __lft = $ maxRgt + 1 ; $ model -> __rgt = $ model -> __lft + 1 ; $ model -> __lvl = 0 ; $ model -> __prt = null ; } else { $ query = ( new Query ( ) ) -> update ( ) -> table ( $ tableName ) -> values ( [ '__lft' => '__lft + :max - :lft + 1' , '__rgt' => '__rgt + :max - :lft + 1' , '__lvl' => '__lvl - :lvl' , ] ) -> where ( '__lft>=:lft AND __rgt<=:rgt' ) -> params ( [ ':max' => $ maxRgt , ':lft' => $ model -> __lft , ':rgt' => $ model -> __rgt , ':lvl' => $ model -> __lvl ] ) ; $ connection -> execute ( $ query ) ; $ this -> removeFromTreeByElement ( $ model ) ; // TODO: calculate new __lft, __rgt! $ model -> refreshTreeColumns ( ) ; $ model -> __lvl = 0 ; $ model -> __prt = null ; } }
Prepares model for insert into tree as last root
@param \T4\Orm\Model $model
Prepares model for insert into tree as last root