[英]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.