예제 #1
0
파일: User.php 프로젝트: levmorozov/mii
 public function update_roles($roles)
 {
     DB::delete('DELETE FROM roles_users WHERE user_id = :id', [':id' => $this->id]);
     foreach ($roles as $role) {
         DB::insert('INSERT INTO roles_users VALUES(:user_id, :role_id)', [':user_id' => $this->id, ':role_id' => $role]);
     }
 }
예제 #2
0
파일: Sort.php 프로젝트: levmorozov/mii
 public function value($parent_field = false)
 {
     if ($parent_field) {
         $value = (new Query())->select([[DB::expr('MAX(' . $this->_field . ')'), $this->_field]])->from($this->_model->get_table())->where($parent_field, '=', $this->_model->get($parent_field))->one();
         if ($value) {
             $value = $value[$this->_field] ?: 0;
         }
     } else {
         $value = (new Query())->select([[DB::expr('MAX(' . $this->_field . ')'), $this->_field]])->from($this->_model->get_table())->one();
         if ($value) {
             $value = $value[$this->_field] ?: 0;
         }
     }
     return $value + 1;
 }
예제 #3
0
파일: Migrate.php 프로젝트: levmorozov/mii
 private function _up($limit = null)
 {
     $applied = 0;
     $migrations = $this->migrations_list;
     $total = count($migrations);
     $limit = (int) $limit;
     if ($limit > 0) {
         $migrations = array_slice($migrations, 0, $limit);
     }
     foreach ($migrations as $migration) {
         if ($migration['applied']) {
             continue;
         }
         $name = $migration['name'];
         $this->info('Loading migration #:name', [':name' => $name]);
         $obj = $this->load_migration($migration);
         $obj->init();
         if ($obj->up() === false) {
             $this->error('Migration #:name failed. Stop.', [':name' => $name]);
             return;
         }
         DB::begin();
         try {
             $obj->safe_up();
             DB::commit();
         } catch (\Throwable $e) {
             DB::rollback();
         }
         DB::insert('INSERT INTO`' . $this->migrate_table . '`(`name`, `date`) VALUES(:name, :date)', [':name' => $name, ':date' => time()]);
         $this->info('Migration up successfully', [':name' => $name]);
         $applied++;
     }
     if (!$applied) {
         $this->warning('No new migration found');
     }
 }
예제 #4
0
파일: Query.php 프로젝트: levmorozov/mii
 public function count()
 {
     $this->_type = Database::SELECT;
     $db = \Mii::$app->db;
     $old_select = $this->_select;
     $old_order = $this->_order_by;
     if ($this->_distinct) {
         $this->select([[DB::expr('COUNT(DISTINCT ' . $db->quote_column($this->_select[0]) . ')'), 'count']]);
     } else {
         $this->select([DB::expr('COUNT(*) AS `count`')]);
     }
     $as_object = $this->_as_object;
     $this->_as_object = null;
     $this->_order_by = [];
     $count = $this->execute()->column('count', 0);
     $this->_select = $old_select;
     $this->_order_by = $old_order;
     $this->_as_object = $as_object;
     return $count;
 }