FixtureController.php 3.65 KB
Newer Older
Mark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\console\controllers;

use Yii;
use yii\console\Controller;
use yii\console\Exception;

/**
 * This command manages fixtures load to the database tables.
 * You can specify different options of this command to point fixture manager
 * to the specific tables of the different database connections.
Qiang Xue committed
18
 *
Mark committed
19
 * To use this command simply configure your console.php config like this:
Qiang Xue committed
20
 *
Mark committed
21
 * ~~~
Qiang Xue committed
22 23 24 25 26 27 28 29 30 31
 * 'db' => [
 *     'class' => 'yii\db\Connection',
 *     'dsn' => 'mysql:host=localhost;dbname={your_database}',
 *     'username' => '{your_db_user}',
 *     'password' => '',
 *     'charset' => 'utf8',
 * ],
 * 'fixture' => [
 *     'class' => 'yii\test\DbFixtureManager',
 * ],
Mark committed
32
 * ~~~
Qiang Xue committed
33
 *
Mark committed
34
 * ~~~
Qiang Xue committed
35 36 37
 * #load fixtures under $fixturePath to the "users" table
 * yii fixture/apply users
 *
Mark committed
38
 * #also a short version of this command (generate action is default)
Qiang Xue committed
39 40 41 42 43 44 45
 * yii fixture users
 *
 * #load fixtures under $fixturePath to the "users" table to the different connection
 * yii fixture/apply users --db='someOtherDbConneciton'
 *
 * #load fixtures under different $fixturePath to the "users" table.
 * yii fixture/apply users --fixturePath='@app/some/other/path/to/fixtures'
Mark committed
46
 * ~~~
Qiang Xue committed
47
 *
Mark committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * @author Mark Jebri <mark.github@yandex.ru>
 * @since 2.0
 */
class FixtureController extends Controller
{

	use \yii\test\DbTestTrait;

	/**
	 * @var string controller default action ID.
	 */
	public $defaultAction = 'apply';

	/**
	 * Alias to the path, where all fixtures are stored.
	 * @var string
	 */
Qiang Xue committed
65
	public $fixturePath = '@tests/unit/fixtures';
Mark committed
66 67 68

	/**
	 * Id of the database connection component of the application.
Qiang Xue committed
69
	 * @var string
Mark committed
70 71 72 73 74 75 76 77 78 79
	 */
	public $db = 'db';

	/**
	 * Returns the names of the global options for this command.
	 * @return array the names of the global options for this command.
	 */
	public function globalOptions()
	{
		return array_merge(parent::globalOptions(), [
Qiang Xue committed
80
			'db', 'fixturePath'
Mark committed
81 82 83 84 85 86
		]);
	}

	/**
	 * This method is invoked right before an action is to be executed (after all possible filters.)
	 * It checks that fixtures path and database connection are available.
Qiang Xue committed
87
	 * @param \yii\base\Action $action
Mark committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	 * @return boolean
	 */
	public function beforeAction($action)
	{
		if (parent::beforeAction($action)) {
			$this->checkRequirements();
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Apply given fixture to the table. Fixture name can be the same as the table name or
	 * you can specify table name as a second parameter.
	 * @param string $fixture
	 */
	public function actionApply($fixture)
	{
Qiang Xue committed
107
		$this->fixtureManager->basePath = $this->fixturePath;
Mark committed
108 109 110 111 112 113 114 115 116 117
		$this->fixtureManager->db = $this->db;
		$this->loadFixtures([$fixture]);
	}

	/**
	 * Truncate given table and clear all fixtures from it.
	 * @param string $table
	 */
	public function actionClear($table)
	{
Qiang Xue committed
118
		$this->getDbConnection()->createCommand()->truncateTable($table)->execute();
Mark committed
119 120 121 122 123 124 125 126 127
		echo "Table \"{$table}\" was successfully cleared. \n";
	}

	/**
	 * Checks if the database and fixtures path are available.
	 * @throws Exception
	 */
	public function checkRequirements()
	{
Qiang Xue committed
128
		$path = Yii::getAlias($this->fixturePath, false);
Mark committed
129 130

		if (!is_dir($path) || !is_writable($path)) {
Qiang Xue committed
131
			throw new Exception("The fixtures path \"{$this->fixturePath}\" not exist or is not writable");
Mark committed
132 133 134 135 136 137
		}

	}

	/**
	 * Returns database connection component
Qiang Xue committed
138
	 * @return \yii\db\Connection|null
Mark committed
139 140 141 142 143 144 145 146 147 148 149 150 151
	 */
	public function getDbConnection()
	{
		$db = Yii::$app->getComponent($this->db);

		if ($db == null) {
			throw new Exception("There is no database connection component with id \"{$this->db}\".");
		}

		return $db;
	}

}