ActiveMetaData.php 3.16 KB
Newer Older
Qiang Xue committed
1 2 3 4 5
<?php

namespace yii\db\ar;

use yii\db\Exception;
Qiang Xue committed
6
use yii\db\dao\TableSchema;
Qiang Xue committed
7 8 9 10 11 12 13 14 15

/**
 * ActiveMetaData represents the meta-data for an Active Record class.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ActiveMetaData
{
Qiang Xue committed
16 17 18 19
	/**
	 * @var ActiveMetaData[] list of ActiveMetaData instances indexed by the model class names
	 */
	public static $instances;
Qiang Xue committed
20 21 22 23
	/**
	 * @var TableSchema the table schema information
	 */
	public $table;
Qiang Xue committed
24 25 26 27
	/**
	 * @var string the model class name
	 */
	public $modelClass;
Qiang Xue committed
28 29 30 31 32
	/**
	 * @var array list of relations
	 */
	public $relations = array();

Qiang Xue committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	/**
	 * Returns an instance of ActiveMetaData for the specified model class.
	 * Note that each model class only has a single ActiveMetaData instance.
	 * This method will only create the ActiveMetaData instance if it is not previously
	 * done so for the specified model class.
	 * @param string $modelClass the model class name. Make sure the class name do NOT have a leading backslash "\".
	 * @param boolean $refresh whether to recreate the ActiveMetaData instance. Defaults to false.
	 * @return ActiveMetaData the ActiveMetaData instance for the specified model class.
	 */
	public static function getInstance($modelClass, $refresh = false)
	{
		if (isset(self::$instances[$modelClass]) && !$refresh) {
			return self::$instances[$modelClass];
		} else {
			return self::$instances[$modelClass] = new self($modelClass);
		}
	}

Qiang Xue committed
51 52 53 54 55 56
	/**
	 * Constructor.
	 * @param string $modelClass the model class name
	 */
	public function __construct($modelClass)
	{
Qiang Xue committed
57
		$this->modelClass = $modelClass;
Qiang Xue committed
58
		$tableName = $modelClass::tableName();
Qiang Xue committed
59 60
		$this->table = $modelClass::getDbConnection()->getDriver()->getTableSchema($tableName);
		if ($this->table === null) {
Qiang Xue committed
61 62
			throw new Exception("Unable to find table '$tableName' for ActiveRecord class '$modelClass'.");
		}
Qiang Xue committed
63 64 65 66 67
		$primaryKey = $modelClass::primaryKey();
		if ($primaryKey !== null) {
			$this->table->fixPrimaryKey($primaryKey);
		} elseif ($this->table->primaryKey === null) {
			throw new Exception("The table '$tableName' for ActiveRecord class '$modelClass' does not have a primary key.");
Qiang Xue committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		}

		foreach ($modelClass::relations() as $name => $config) {
			$this->addRelation($name, $config);
		}
	}

	/**
	 * Adds a relation.
	 *
	 * $config is an array with three elements:
	 * relation type, the related active record class and the foreign key.
	 *
	 * @throws Exception
	 * @param string $name $name Name of the relation.
	 * @param array $config $config Relation parameters.
	 * @return void
	 */
	public function addRelation($name, $config)
	{
		if (preg_match('/^(\w+)\s*:\s*\\\\?([\w\\\\]+)(\[\])?$/', $name, $matches)) {
			if (is_string($config)) {
				$config = array('on' => $config);
			}
Qiang Xue committed
92
			$relation = new ActiveRelation($config);
Qiang Xue committed
93
			$relation->name = $matches[1];
Qiang Xue committed
94 95
			$modelClass = $matches[2];
			if (strpos($modelClass, '\\') !== false) {
Qiang Xue committed
96
				$relation->modelClass = ltrim($modelClass, '\\');
Qiang Xue committed
97 98 99
			} else {
				$relation->modelClass = dirname($this->modelClass) . '\\' . $modelClass;
			}
Qiang Xue committed
100 101 102
			$relation->hasMany = isset($matches[3]);
			$this->relations[$relation->name] = $relation;
		} else {
Qiang Xue committed
103
			throw new Exception("{$this->modelClass} has an invalid relation: $name");
Qiang Xue committed
104 105 106
		}
	}
}