public function run() { $config = $this->app->database; $dsn = sprintf('%s:host=%s;port=%s', $config['type'], $config['host'], $config['port']); $pdo = new PDO($dsn, $config['username'], $config['password'], $config['options']); try { if ($this->params['force']) { // Force create database $pdo->exec('DROP DATABASE `' . $config['dbname'] . '`'); $pdo->exec('CREATE DATABASE `' . $config['dbname'] . '` DEFAULT CHARACTER SET `utf8`;'); } else { // Try to create database $pdo->exec('CREATE DATABASE IF NOT EXISTS `' . $config['dbname'] . '` DEFAULT CHARACTER SET `utf8`;'); } $pdo->exec("USE `{$config['dbname']}`"); $sql = file_get_contents(APP_DIR . '/migrations/schema.sql'); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); $pdo->exec($sql); if ($this->params['data']) { $sql = file_get_contents(APP_DIR . '/migrations/data.sql'); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); $pdo->exec($sql); } } catch (\PDOException $e) { print Cli::text('Create database error: ' . $e->getMessage() . PHP_EOL, 'red'); $this->output->end(); } print Cli::text('Init database ok' . PHP_EOL, 'green'); }
public function run() { $articles = Model::factory('Article')->find_many(); foreach ($articles as $article) { $article->comments_count = $article->comments()->count(); $article->digg_count = Model::factory('UserDigg')->where('article_id', $article->id)->count(); $article->save(); } print Cli::text('Update article count ok' . PHP_EOL, 'green'); }
public function run() { $users = Model::factory('User')->find_many(); foreach ($users as $user) { $user->posts_count = $user->articles()->where('status', Article::OK)->count(); $user->digged_count = 0; foreach ($user->articles()->where('status', Article::OK)->find_many() as $article) { $user->digged_count += $article->digg_count; } $user->save(); } print Cli::text('Update user count ok' . PHP_EOL, 'green'); }
public function run() { $model = Model::factory('Article'); if (!$this->params['all']) { $model->where_gte('created_at', date('Y-m-d', strtotime('-1 month'))); } /** @var $articles \Model\Article[] */ $articles = $model->find_many(); foreach ($articles as $article) { $article->digg_count = $article->diggs()->count(); $article->comments_count = $article->comments()->count(); $article->point = $article->analysisPoint(); $article->save(); } print Cli::text('Analysis article point ok' . PHP_EOL, 'green'); }
public function run() { $pdo = $this->app->pdo; $config = $this->app->get("database"); try { if ($this->params['force']) { // Force create database $pdo->exec('DROP DATABASE `' . $config['dbname'] . '`'); $pdo->exec('CREATE DATABASE `' . $config['dbname'] . '` DEFAULT CHARACTER SET `utf8`;'); } else { // Try to create database $pdo->exec('CREATE DATABASE IF NOT EXISTS `' . $config['dbname'] . '` DEFAULT CHARACTER SET `utf8`;'); } $pdo->exec("USE `{$config['dbname']}`"); $sql = file_get_contents(APP_DIR . '/migrations/schema.sql'); if (!$sql) { print Cli::text('migrations/schema.sql is empty' . PHP_EOL, 'red'); } else { $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); $pdo->exec($sql); } if ($this->params['data'] && is_file(APP_DIR . '/migrations/data.sql')) { $sql = file_get_contents(APP_DIR . '/migrations/data.sql'); if (!$sql) { print Cli::text('migrations/data.sql is empty' . PHP_EOL, 'red'); } else { $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0); $pdo->exec($sql); } } } catch (\PDOException $e) { print Cli::text('Create database error: ' . $e->getMessage() . PHP_EOL, 'red'); $this->output->end(); } print Cli::text('Init database ok' . PHP_EOL, 'green'); }
public function run() { $post = Model::factory('Article')->find_one($this->params['id']); if (!$post) { echo Cli::text('Article is not exists' . PHP_EOL, 'red'); } echo 'Article is ' . PHP_EOL . PHP_EOL . "\t" . Cli::text($post->title, 'green') . PHP_EOL . PHP_EOL; switch ($this->input->param('action')) { case 'delete': if (!Cli::confirm('Do you want to delete')) { return; } $post->status = Article::DELETED; $post->save(); break; case 'revert': $post->status = Article::OK; $post->save(); break; default: echo Cli::text('Error action!', 'red') . PHP_EOL; } echo 'Action done!' . PHP_EOL; }