简体   繁体   English

$ _schema是否在锂父子模型中继承?

[英]Does $_schema get inherited in Lithium parent & child Model?

We all know that functions are inherited, but how about the protected $_schema of Lithium Model? 我们都知道函数是继承的,但是受锂模型保护的$_schema呢?

For example, I have: 例如,我有:

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'),
    );
}

I wonder when saving a ChildModel record, would the $_schema of ChildModel combined with the $_schema of ParentModel ? 我想保存时ChildModel记录,将在$_schemaChildModel与合并$_schemaParentModel That's: 那是:

array(
    'name' => array('type' => 'string'),
    'address' => array('type' => 'string'),
    'mobile' => array('type' => 'string'),
    'email' => array('type' => 'string'),
);

How can I check if this is the case? 如何检查是否是这种情况?

Big Thanks 太谢谢了

Typically in PHP, variables defined this way will override the parent class' default value for the same class. 通常,在PHP中,以这种方式定义的变量将覆盖同一类的父类的默认值。 However, Lithium models have code that iterates through parents and merges in their defaults for $_schema and all other variables listed in $_inherits and the defaults returned by Model::_inherited() . 然而,锂电池型号有代码 ,通过父母迭代,并在其默认为合并$_schema和中列出的所有其他变量$_inherits和返回的默认Model::_inherited()

Here is the code for this in the 1.0-beta release 这是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'
    ));
}

Here are some of the unit tests that cover this functionality: https://github.com/UnionOfRAD/lithium/blob/1.0-beta/tests/cases/data/ModelTest.php#L211-L271 以下是一些涵盖此功能的单元测试: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM