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

namespace yii\test;

use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;

/**
Qiang Xue committed
15
 * DbFixture is the base class for DB-related fixtures.
Qiang Xue committed
16
 *
Qiang Xue committed
17
 * DbFixture provides the [[db]] connection to be used by DB fixtures.
Qiang Xue committed
18 19 20 21
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
22
abstract class DbFixture extends Fixture
Qiang Xue committed
23 24 25 26 27 28 29 30
{
	/**
	 * @var Connection|string the DB connection object or the application component ID of the DB connection.
	 * After the DbFixture object is created, if you want to change this property, you should only assign it
	 * with a DB connection object.
	 */
	public $db = 'db';

Carsten Brandt committed
31

Qiang Xue committed
32 33 34 35 36 37 38 39 40
	/**
	 * @inheritdoc
	 */
	public function init()
	{
		parent::init();
		if (is_string($this->db)) {
			$this->db = Yii::$app->getComponent($this->db);
		}
Qiang Xue committed
41
		if (!is_object($this->db)) {
Qiang Xue committed
42 43 44 45
			throw new InvalidConfigException("The 'db' property must be either a DB connection instance or the application component ID of a DB connection.");
		}
	}
}