-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from CottaCush/features/add-base-console-contr…
…oller Feature: Add base console controller
- Loading branch information
Showing
2 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |