如何在Cakephp的扩展模型中继承AppModel的$belongsTo
在Cakephp中, 我在AppModel中使用了belongsTo如何在Cakephp的扩展模型中继承AppModel的$belongsTo,cakephp,inheritance,model,associations,Cakephp,Inheritance,Model,Associations,在Cakephp中, 我在AppModel中使用了belongsTo class AppModel extends Model { public $actsAs = array('Containable'); public $belongsTo = array( 'ModifiedBy' => array( 'className' => 'User', 'foreignKey' => 'commo
class AppModel extends Model {
public $actsAs = array('Containable');
public $belongsTo = array(
'ModifiedBy' => array(
'className' => 'User',
'foreignKey' => 'common_modified_by_user_id'
)
);
}
现在,我还想在UserModel中应用属于
class User extends AppModel {
public $belongsTo = array(
'School' => array(
'className' => 'User',
'foreignKey' => 'school_id'
)
);
}
但当我这么做的时候……它超越了应用程序模型的属性,学校数据进入了获取的数组
请帮助…没有自动合并关联
除了$actsAs
和$findMethods
变量之外,关联配置不会自动合并,您必须自己进行合并
解决这个问题应该相当简单,在用户中使用对象::\u mergeVars()
模型构造函数应该做到以下几点:
public function __construct($id = false, $table = null, $ds = null) {
$parent = get_parent_class($this);
$this->_mergeVars(array('belongsTo'), $parent);
parent::__construct($id, $table, $ds);
}
注:您可能想看一看,它们可能是满足您需求的更好选择。如果您已经用给出的答案解决了这个问题,您能接受吗?或者,如果它没有提供解决方案,请发布新的详细信息来帮助您。将此构造函数放到appModel.php
中如何??从逻辑上讲,它应该可以正常工作,但我忽略了它的缺点吗?除了数据库中的表可能存在而没有这些colunm抛出异常之外……还有什么别的吗?