CActiveRecordMetaData
鍖 | system.db.ar |
---|---|
缁ф壙 | class CActiveRecordMetaData |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CActiveRecord.php 3533 2012-01-08 22:07:55Z mdomba $ |
婧愮爜 | framework/db/ar/CActiveRecord.php |
CActiveRecordMetaData琛ㄧず涓涓椿鍔ㄨ褰曠被鐨勬簮鏁版嵁銆
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
attributeDefaults | array | 灞炴ч粯璁ゅ | CActiveRecordMetaData |
columns | array | 琛ㄧ殑鍒 | CActiveRecordMetaData |
relations | array | 鍏崇郴鍒楄〃 | CActiveRecordMetaData |
tableSchema | CDbTableSchema | 琛ㄧ粨鏋勪俊鎭 | CActiveRecordMetaData |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__construct() | 鏋勯犲嚱鏁般 | CActiveRecordMetaData |
addRelation() | 澧炲姞涓涓叧绯汇 | CActiveRecordMetaData |
hasRelation() | 妫鏌ユ槸鍚︽湁涓涓甫鏈夋寚瀹氬悕绉板畾涔夌殑鍏崇郴銆 | CActiveRecordMetaData |
removeRelation() | 鍒犻櫎涓涓甫鏈夋寚瀹氬悕绉扮殑鍏崇郴銆 | CActiveRecordMetaData |
灞炴ц缁
attributeDefaults
灞炴
public array $attributeDefaults;
灞炴ч粯璁ゅ
columns
灞炴
public array $columns;
琛ㄧ殑鍒
relations
灞炴
public array $relations;
鍏崇郴鍒楄〃
tableSchema
灞炴
public CDbTableSchema $tableSchema;
琛ㄧ粨鏋勪俊鎭
鏂规硶璇︾粏
__construct()
鏂规硶
public void __construct(CActiveRecord $model)
| ||
$model | CActiveRecord | 妯″瀷瀹炰緥 |
婧愮爜锛 framework/db/ar/CActiveRecord.php#2258 (鏄剧ず)
public function __construct($model)
{
$this->_model=$model;
$tableName=$model->tableName();
if(($table=$model->getDbConnection()->getSchema()->getTable($tableName))===null)
throw new CDbException(Yii::t('yii','The table "{table}" for active record class "{class}" cannot be found in the database.',
array('{class}'=>get_class($model),'{table}'=>$tableName)));
if($table->primaryKey===null)
{
$table->primaryKey=$model->primaryKey();
if(is_string($table->primaryKey) && isset($table->columns[$table->primaryKey]))
$table->columns[$table->primaryKey]->isPrimaryKey=true;
else if(is_array($table->primaryKey))
{
foreach($table->primaryKey as $name)
{
if(isset($table->columns[$name]))
$table->columns[$name]->isPrimaryKey=true;
}
}
}
$this->tableSchema=$table;
$this->columns=$table->columns;
foreach($table->columns as $name=>$column)
{
if(!$column->isPrimaryKey && $column->defaultValue!==null)
$this->attributeDefaults[$name]=$column->defaultValue;
}
foreach($model->relations() as $name=>$config)
{
$this->addRelation($name,$config);
}
}
鏋勯犲嚱鏁般
addRelation()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.2锛
public void addRelation(string $name, array $config)
| ||
$name | string | $name 鍏崇郴鍚嶇О銆 |
$config | array | $config 鐩稿叧鍙傛暟銆 |
{return} | void |
婧愮爜锛 framework/db/ar/CActiveRecord.php#2307 (鏄剧ず)
public function addRelation($name,$config)
{
if(isset($config[0],$config[1],$config[2])) // relation class, AR class, FK
$this->relations[$name]=new $config[0]($name,$config[1],$config[2],array_slice($config,3));
else
throw new CDbException(Yii::t('yii','Active record "{class}" has an invalid configuration for relation "{relation}". It must specify the relation type, the related active record class and the foreign key.', array('{class}'=>get_class($this->_model),'{relation}'=>$name)));
}
澧炲姞涓涓叧绯汇
$config 鏄竴涓甫鏈変笁涓厓绱犵殑鏁扮粍锛
鍏崇郴绫诲瀷锛岀浉鍏崇殑娲诲姩璁板綍绫诲拰澶栭敭銆
hasRelation()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.2锛
public boolean hasRelation(string $name)
| ||
$name | string | $name 鍏崇郴鐨勫悕绉般 |
{return} | boolean |
婧愮爜锛 framework/db/ar/CActiveRecord.php#2322 (鏄剧ず)
public function hasRelation($name)
{
return isset($this->relations[$name]);
}
妫鏌ユ槸鍚︽湁涓涓甫鏈夋寚瀹氬悕绉板畾涔夌殑鍏崇郴銆
removeRelation()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.2锛
public void removeRelation(string $name)
| ||
$name | string | $name |
{return} | void |
婧愮爜锛 framework/db/ar/CActiveRecord.php#2334 (鏄剧ず)
public function removeRelation($name)
{
unset($this->relations[$name]);
}
鍒犻櫎涓涓甫鏈夋寚瀹氬悕绉扮殑鍏崇郴銆