[英]Does $_schema get inherited in Lithium parent & child Model?
我们都知道函数是继承的,但是受锂模型保护的$_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
记录,将在$_schema
的ChildModel
与合并$_schema
的ParentModel
? 那是:
array(
'name' => array('type' => 'string'),
'address' => array('type' => 'string'),
'mobile' => array('type' => 'string'),
'email' => array('type' => 'string'),
);
如何检查是否是这种情况?
太谢谢了
通常,在PHP中,以这种方式定义的变量将覆盖同一类的父类的默认值。 然而,锂电池型号有代码 ,通过父母迭代,并在其默认为合并$_schema
和中列出的所有其他变量$_inherits
和返回的默认Model::_inherited()
。
这是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
正如您打开的github问题所回答的,是的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.