Skip to content

Commit

Permalink
Merge pull request #14 from CottaCush/features/add-base-console-contr…
Browse files Browse the repository at this point in the history
…oller

Feature: Add base console controller
  • Loading branch information
goke-epapa authored Jun 8, 2017
2 parents a7fe79d + 4a1df86 commit eeb103c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

#### 1.9.0

* Add base console controller *2017-06-08*

#### 1.8.0

* Support PHP version 7 *2017-06-05*
* Add base controller, model and text utils *2017-06-05*

#### 1.7.0

Expand Down
120 changes: 120 additions & 0 deletions src/Controller/BaseConsoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace CottaCush\Yii2\Controller;

use yii\console\Controller;
use yii\db\Connection;
use yii\db\Query;
use yii\db\Transaction;

/**
* Class BaseConsoleController
* @package CottaCush\Yii2\Controller
* @author Olawale Lawal <[email protected]>
* @author Adeyemi Olaoye <[email protected]>
* @codeCoverageIgnore
*/
class BaseConsoleController extends Controller
{
/** @var Connection */
protected $db;

/** @var Transaction */
protected $transaction;

/**
* @author Olawale Lawal <[email protected]>
* @param \yii\base\Action $action
* @return bool
*/
public function beforeAction($action)
{
if (is_null($this->db)) {
$this->db = \Yii::$app->db;
}
return parent::beforeAction($action);
}

/**
* @author Olawale Lawal <[email protected]>
*/
public function beginTransaction()
{
$this->transaction = $this->db->beginTransaction();
}

/**
* @author Olawale Lawal <[email protected]>
*/
public function commitTransaction()
{
$this->transaction->commit();
}

/**
* @author Olawale Lawal <[email protected]>
*/
public function rollbackTransaction()
{
$this->transaction->rollBack();
}

/**
* @author Olawale Lawal <[email protected]>
* @param $table
* @param $field
* @param $value
* @param string $returnColumn
* @return false|null|string
*/
public static function getColumnByField($table, $field, $value, $returnColumn = 'id')
{
return (new Query)->select($returnColumn)
->from($table)->filterWhere([$field => $value])
->scalar();
}

public function getRandomOne($table, $column, $conditions = [])
{
return (new Query)->select($column)
->from($table)->filterWhere($conditions)->orderBy('rand()')->one();
}

public function getAll($table, $columns, $conditions = [])
{
return (new Query)->select($columns)
->from($table)->andFilterWhere($conditions)->all();
}

/**
* @author Olawale Lawal <[email protected]>
* @param $table
* @param array $columns
* @return int
*/
public function insert($table, array $columns)
{
return $this->db->createCommand()->insert($table, $columns)->execute();
}

/**
* @author Olawale Lawal <[email protected]>
* @param $table
* @param array $columns
* @param array $rows
* @return int
*/
public function batchInsert($table, array $columns, array $rows)
{
if (empty($rows)) {
return true;
}

return $this->db->createCommand()->batchInsert($table, $columns, $rows)->execute();
}

public function stdout($string)
{
parent::stdout($string . PHP_EOL);
}
}

0 comments on commit eeb103c

Please sign in to comment.