CActiveRecordMetaData
包 | system.db.ar |
---|---|
继承 | class CActiveRecordMetaData |
可用自 | 1.0 |
源码 | framework/db/ar/CActiveRecord.php |
CActiveRecordMetaData represents the meta-data for an Active Record class.
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
attributeDefaults | array | attribute default values | CActiveRecordMetaData |
columns | array | table columns | CActiveRecordMetaData |
relations | array | list of relations | CActiveRecordMetaData |
tableSchema | CDbTableSchema | the table schema information | CActiveRecordMetaData |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__construct() | Constructor. | CActiveRecordMetaData |
addRelation() | Adds a relation. | CActiveRecordMetaData |
hasRelation() | Checks if there is a relation with specified name defined. | CActiveRecordMetaData |
removeRelation() | Deletes a relation with specified name. | CActiveRecordMetaData |
属性详情
attributeDefaults
属性
public array $attributeDefaults;
attribute default values
columns
属性
public array $columns;
table columns
relations
属性
public array $relations;
list of relations
tableSchema
属性
public CDbTableSchema $tableSchema;
the table schema information
方法详情
__construct()
方法
public void __construct(CActiveRecord $model)
| ||
$model | CActiveRecord | the model instance |
源码: framework/db/ar/CActiveRecord.php#2384 (显示)
public function __construct($model)
{
$this->_modelClassName=get_class($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}'=>$this->_modelClassName,'{table}'=>$tableName)));
if(($modelPk=$model->primaryKey())!==null || $table->primaryKey===null)
{
$table->primaryKey=$modelPk;
if(is_string($table->primaryKey) && isset($table->columns[$table->primaryKey]))
$table->columns[$table->primaryKey]->isPrimaryKey=true;
elseif(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);
}
}
Constructor.
addRelation()
方法
(自版本 v1.1.2 可用)
public void addRelation(string $name, array $config)
| ||
$name | string | $name Name of the relation. |
$config | array | $config Relation parameters. |
{return} | void |
源码: framework/db/ar/CActiveRecord.php#2434 (显示)
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}'=>$this->_modelClassName,'{relation}'=>$name)));
}
Adds a relation.
$config is an array with three elements:
relation type, the related active record class and the foreign key.
hasRelation()
方法
(自版本 v1.1.2 可用)
public boolean hasRelation(string $name)
| ||
$name | string | $name Name of the relation. |
{return} | boolean |
源码: framework/db/ar/CActiveRecord.php#2449 (显示)
public function hasRelation($name)
{
return isset($this->relations[$name]);
}
Checks if there is a relation with specified name defined.
removeRelation()
方法
(自版本 v1.1.2 可用)
public void removeRelation(string $name)
| ||
$name | string | $name |
{return} | void |
源码: framework/db/ar/CActiveRecord.php#2461 (显示)
public function removeRelation($name)
{
unset($this->relations[$name]);
}
Deletes a relation with specified name.