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]); } }
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; }
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'); } }
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; }