$_schema 是否在 Lithium 父子模型中继承?
Does $_schema get inherited in Lithium parent & child Model?
我们都知道功能是可以继承的,但是Lithium Model的protected$_schema
呢?
例如,我有:
class ParentModel extends Model {
protected $_schema = array(
'name' => array('type' => 'string'),
'address' => array('type' => 'string'),
);
}
class ChildModel extends ParentModel {
protected $_schema = array(
'mobile' => array('type' => 'string'),
'email' => array('type' => 'string'),
);
}
我想知道保存ChildModel
记录时,ChildModel
的$_schema
会和ParentModel
的$_schema
合并吗?那是:
array(
'name' => array('type' => 'string'),
'address' => array('type' => 'string'),
'mobile' => array('type' => 'string'),
'email' => array('type' => 'string'),
);
如何检查是否是这种情况?
非常感谢
正如在您打开的 github 问题上的回答,是的。
通常在 PHP 中,以这种方式定义的变量将覆盖父 class 相同 class 的默认值。但是,Lithium 模型 have code 遍历父级并合并 $_schema
和 $_inherits
中列出的所有其他变量的默认值以及 Model::_inherited()
.[=17= 返回的默认值]
这里是 the code 1.0-beta 版本
/**
* Merge parent class attributes to the current instance.
*/
protected function _inherit() {
$inherited = array_fill_keys($this->_inherited(), array());
foreach (static::_parents() as $parent) {
$parentConfig = get_class_vars($parent);
foreach ($inherited as $key => $value) {
if (isset($parentConfig["{$key}"])) {
$val = $parentConfig["{$key}"];
if (is_array($val)) {
$inherited[$key] += $val;
}
}
}
if ($parent === __CLASS__) {
break;
}
}
foreach ($inherited as $key => $value) {
if (is_array($this->{$key})) {
$this->{$key} += $value;
}
}
}
/**
* Return inherited attributes.
*
* @param array
*/
protected function _inherited() {
return array_merge($this->_inherits, array(
'validates',
'belongsTo',
'hasMany',
'hasOne',
'_meta',
'_finders',
'_query',
'_schema',
'_classes',
'_initializers'
));
}
以下是涵盖此功能的一些单元测试:https://github.com/UnionOfRAD/lithium/blob/1.0-beta/tests/cases/data/ModelTest.php#L211-L271
我们都知道功能是可以继承的,但是Lithium Model的protected$_schema
呢?
例如,我有:
class ParentModel extends Model {
protected $_schema = array(
'name' => array('type' => 'string'),
'address' => array('type' => 'string'),
);
}
class ChildModel extends ParentModel {
protected $_schema = array(
'mobile' => array('type' => 'string'),
'email' => array('type' => 'string'),
);
}
我想知道保存ChildModel
记录时,ChildModel
的$_schema
会和ParentModel
的$_schema
合并吗?那是:
array(
'name' => array('type' => 'string'),
'address' => array('type' => 'string'),
'mobile' => array('type' => 'string'),
'email' => array('type' => 'string'),
);
如何检查是否是这种情况?
非常感谢
正如在您打开的 github 问题上的回答,是的。
通常在 PHP 中,以这种方式定义的变量将覆盖父 class 相同 class 的默认值。但是,Lithium 模型 have code 遍历父级并合并 $_schema
和 $_inherits
中列出的所有其他变量的默认值以及 Model::_inherited()
.[=17= 返回的默认值]
这里是 the code 1.0-beta 版本
/**
* Merge parent class attributes to the current instance.
*/
protected function _inherit() {
$inherited = array_fill_keys($this->_inherited(), array());
foreach (static::_parents() as $parent) {
$parentConfig = get_class_vars($parent);
foreach ($inherited as $key => $value) {
if (isset($parentConfig["{$key}"])) {
$val = $parentConfig["{$key}"];
if (is_array($val)) {
$inherited[$key] += $val;
}
}
}
if ($parent === __CLASS__) {
break;
}
}
foreach ($inherited as $key => $value) {
if (is_array($this->{$key})) {
$this->{$key} += $value;
}
}
}
/**
* Return inherited attributes.
*
* @param array
*/
protected function _inherited() {
return array_merge($this->_inherits, array(
'validates',
'belongsTo',
'hasMany',
'hasOne',
'_meta',
'_finders',
'_query',
'_schema',
'_classes',
'_initializers'
));
}
以下是涵盖此功能的一些单元测试:https://github.com/UnionOfRAD/lithium/blob/1.0-beta/tests/cases/data/ModelTest.php#L211-L271