我们都知道函数是继承的,但是锂模型的受保护$_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'),
);
如何检查是否是这种情况?
非常感谢
通常在 PHP 中,以这种方式定义的变量将覆盖同一类的父类默认值。但是,Lithium 模型具有循环访问父项的代码,并合并其默认值$_schema
和 $_inherits
中列出的所有其他变量以及 Model::_inherited()
返回的默认值。
这是 1.0 测试版中的代码
/**
* 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
正如您打开的 github 问题的回答,是的。