コード例 #1
0
ファイル: Blocks.php プロジェクト: scorp7mix/try.t4
 public function actionSetupBlock($sectionId, $blockPath)
 {
     $query = new QueryBuilder();
     $query->select('MAX(' . Block::getDbDriver()->quoteName('order') . ')')->from(Block::getTableName())->where('section=:section')->params([':section' => $sectionId]);
     $maxOrder = (int) $this->app->db->default->query($query)->fetchScalar();
     $block = new Block();
     $block->section = $sectionId;
     $block->path = $blockPath;
     $block->template = '';
     $params = [];
     foreach ($this->app->config->blocks->{$blockPath}->options as $optionName => $options) {
         $params[$optionName] = $options->default;
     }
     $block->options = json_encode($params);
     $block->order = $maxOrder + 10;
     if (false !== $block->save()) {
         $this->data->id = $block->getPK();
         $this->data->result = true;
     } else {
         $this->data->result = false;
     }
 }
コード例 #2
0
ファイル: Pgsql.php プロジェクト: enerjiser2014/t4
 public function countAllByColumn($class, $column, $value, $options = [])
 {
     $query = new QueryBuilder();
     $query->select('COUNT(*)')->from($class::getTableName())->where('`' . $column . '`=:value')->params([':value' => $value]);
     return $class::getDbConnection()->query($query->getQuery($this), $query->getParams())->fetchScalar();
 }
コード例 #3
0
ファイル: Migrate.php プロジェクト: RayManOff/t4
 protected function getLastMigrationTime()
 {
     $query = new QueryBuilder();
     $query->select('time')->from(self::TABLE_NAME)->order(Model::PK . ' DESC')->limit(1);
     return $this->app->db->default->query($query)->fetchScalar() ?: 0;
 }
コード例 #4
0
ファイル: TRelations.php プロジェクト: RayManOff/t4
 /**
  * "Ленивое" получение данных связи для моделей
  * @param string $key
  * @param array $options
  * @return mixed
  * @throws Exception
  */
 public function getRelationLazy($key, $options = [])
 {
     $class = get_class($this);
     $relations = $class::getRelations();
     if (empty($relations[$key])) {
         throw new Exception('No such column or relation: ' . $key . ' in model of ' . $class . ' class');
     }
     $relation = $relations[$key];
     switch ($relation['type']) {
         case $class::BELONGS_TO:
             $relationClass = $relation['model'];
             $link = $class::getRelationLinkName($relation);
             $subModel = $relationClass::findByPK($this->{$link});
             if (empty($subModel)) {
                 return null;
             } else {
                 return $relationClass::findByPK($this->{$link});
             }
             break;
         case $class::HAS_ONE:
             $relationClass = $relation['model'];
             $link = $class::getRelationLinkName($relation);
             return $relationClass::findByColumn($link, $this->getPk(), $options);
             break;
         case $class::HAS_MANY:
             $relationClass = $relation['model'];
             $link = $class::getRelationLinkName($relation);
             return $relationClass::findAllByColumn($link, $this->getPk(), $options);
             break;
         case $class::MANY_TO_MANY:
             $relationClass = $relation['model'];
             $linkTable = $class::getRelationLinkName($relation);
             $pivots = $relationClass::getPivots($class, $key);
             if (!empty($pivots)) {
                 $pivotColumnsSql = ', ' . implode(', ', array_map(function ($x) {
                     return 'j1.' . $x;
                 }, array_keys($pivots)));
             } else {
                 $pivotColumnsSql = '';
             }
             $query = new QueryBuilder();
             $query->select('t1.*' . $pivotColumnsSql)->from($relationClass::getTableName())->join($linkTable, 't1.' . $relationClass::PK . '=j1.' . static::getManyToManyThatLinkColumnName($relation), 'right')->where('(j1.' . static::getManyToManyThisLinkColumnName() . '=:id)' . (isset($options['where']) ? ' AND (' . $options['where'] . ')' : ''));
             if (isset($options['order'])) {
                 $query->order($options['order']);
             }
             $query->params([':id' => $this->getPk()]);
             $result = $relationClass::getDbConnection()->query($query)->fetchAll(\PDO::FETCH_CLASS, $relationClass);
             if (!empty($result)) {
                 $ret = new Collection($result);
                 $ret->setNew(false);
                 return $ret;
             } else {
                 return new Collection();
             }
     }
 }
コード例 #5
0
ファイル: TTreeMagic.php プロジェクト: RayManOff/t4
 public function __call($method, $argv)
 {
     /** @var \T4\Orm\Model $model */
     $model = $argv[0];
     array_shift($argv);
     /* @var \T4\Orm\Model $class */
     $class = get_class($model);
     $tableName = $class::getTableName();
     /** @var \T4\Dbal\Connection $connection */
     $connection = $class::getDbConnection();
     switch ($method) {
         case 'refreshTreeColumns':
             $sql = new QueryBuilder();
             $sql->select(['__lft', '__rgt', '__lvl', '__prt'])->from($tableName)->where('t1.' . $class::PK . '=:id');
             $columns = $connection->query($sql, [':id' => $model->getPk()])->fetch();
             $model->merge($columns);
             return $model;
         case 'getTreeWidth':
             if ($model->isNew()) {
                 return 1;
             } else {
                 return $model->__rgt - $model->__lft;
             }
         case 'findAllParents':
             $query = new QueryBuilder();
             $query->select('*')->from($class::getTableName())->where('__lft<:lft AND __rgt>:rgt')->order('__lft')->params([':lft' => $model->__lft, ':rgt' => $model->__rgt]);
             return $class::findAllByQuery($query);
         case 'findAllChildren':
             $query = new QueryBuilder();
             $query->select('*')->from($class::getTableName())->where('__lft>:lft AND __rgt<:rgt')->order('__lft')->params([':lft' => $model->__lft, ':rgt' => $model->__rgt]);
             return $class::findAllByQuery($query);
         case 'hasChildren':
             $query = new QueryBuilder();
             $query->select('COUNT(*)')->from($class::getTableName())->where('__lft>:lft AND __rgt<:rgt')->order('__lft')->params([':lft' => $model->__lft, ':rgt' => $model->__rgt]);
             return 0 != $connection->query($query)->fetchScalar();
         case 'findSubTree':
             $query = new QueryBuilder();
             $query->select('*')->from($class::getTableName())->where('__lft>=:lft AND __rgt<=:rgt')->order('__lft')->params([':lft' => $model->__lft, ':rgt' => $model->__rgt]);
             return $class::findAllByQuery($query);
         case 'hasPrevSibling':
             $query = new QueryBuilder();
             $query->select('COUNT(*)')->from($class::getTableName())->where('__rgt<:lft AND __prt=:prt')->params([':lft' => $model->__lft, ':prt' => $model->__prt]);
             return 0 != $connection->query($query)->fetchScalar();
         case 'getPrevSibling':
             $query = new QueryBuilder();
             $query->select('*')->from($class::getTableName())->where('__rgt<:lft AND __prt=:prt')->order('__lft DESC')->limit(1)->params([':lft' => $model->__lft, ':prt' => $model->__prt]);
             return $class::findByQuery($query);
         case 'hasNextSibling':
             $query = new QueryBuilder();
             $query->select('COUNT(*)')->from($class::getTableName())->where('__lft>:rgt AND __prt=:prt')->params([':rgt' => $model->__rgt, ':prt' => $model->__prt]);
             return 0 != $connection->query($query)->fetchScalar();
         case 'getNextSibling':
             $query = new QueryBuilder();
             $query->select('*')->from($class::getTableName())->where('__lft>:rgt AND __prt=:prt')->order('__lft')->limit(1)->params([':rgt' => $model->__rgt, ':prt' => $model->__prt]);
             return $class::findByQuery($query);
         case 'insertBefore':
             $element = $argv[0];
             $this->insertModelBeforeElement($model, $element);
             return $model;
             break;
         case 'insertAfter':
             $element = $argv[0];
             $this->insertModelAfterElement($model, $element);
             return $model;
             break;
         case 'moveToFirstPosition':
             $parent = $model->parent;
             if (empty($parent)) {
                 $this->insertModelAsFirstRoot($model);
             } else {
                 $this->insertModelAsFirstChildOf($model, $parent);
             }
             return $model;
         case 'moveToLastPosition':
             $parent = $model->parent;
             if (empty($parent)) {
                 $this->insertModelAsLastRoot($model);
             } else {
                 $this->insertModelAsLastChildOf($model, $parent);
             }
             return $model;
     }
 }
コード例 #6
0
ファイル: Tree.php プロジェクト: RayManOff/t4
 /**
  * Подготовка модели к вставке в дерево в качестве последнего корня
  * @param \T4\Orm\Model $model
  */
 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 QueryBuilder();
     $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 = 0;
     } else {
         $query = new QueryBuilder();
         $query->update()->table($tableName)->values(['__lft' => '__lft + :max - :lft + 1', '__rgt' => '__rgt + :max - :lft + 1', '__lvl' => '__lvl - :lvl'])->where('__lft>=:lft AND __rgt<=:rgt');
         $query->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 = 0;
     }
 }