$ _schema是否继承在Lithium parent&儿童模特?

时间:2015-01-09 04:42:07

标签: php inheritance lithium

我们都知道函数是继承的,但是锂模型的受保护$_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的{​​{1}}是否与ChildModel的{​​{1}}相结合?也就是说' S:

$_schema

我如何检查是否是这种情况?

非常感谢

2 个答案:

答案 0 :(得分:3)

通常在PHP中,以这种方式定义的变量将覆盖父类'同一个类的默认值。但是,Lithium模型have code会迭代父项并合并其$_schema的默认值以及$_inherits中列出的所有其他变量以及Model::_inherited()返回的默认值。

这是1.0-beta版本中的the code

/**
 * 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

答案 1 :(得分:0)

在你打开的github问题上回答,是的。