我有一个模型我想检查更多的插件,如果插件加载然后将插件中的模型附加到man模型中。我用这种方法?但模型和行动的结果不同。 另一种方法比用于插件检查的更多绑定构造更好。
class Comment extends AppModel {
/**
* @see Model::$belongsTo
*/
public $belongsTo = array(
'Content' => array(
'className' => 'Content',
'foreignKey' => 'object_id',
'conditions' => array(
'Comment.object_id = Content.id',
)
),
);
/**
* @see Model::__construct
*/
public function __construct($id = false, $table = null, $ds = null) {
// parent
parent::__construct($id, $table, $ds);
// check for newsstudio
if (CakePlugin::loaded('NewModel')) {
$this->bindModel(
array('belongsTo' => array(
'NewModel' => array(
'className' => 'NewModel.NewModel',
'foreignKey' => 'object_id',
'conditions' => array(
'Comment.object_id = NewModel.id',
)
)
)
));
}
var_dump($this->belongsTo); // correct! NewModel added to blongsto
}
}
// but in action during use. Plugin loaded but
var_dump($this->Comment->belongsTo); // incorrect! just `Content` added
答案 0 :(得分:0)
考虑到你是在__construct中进行的,你可以在调用父节点之前将其添加到$belongsTo
属性,这样可以节省一些CPU周期,因为你没有进行额外的方法调用。
class Comment extends AppModel {
/**
* @see Model::$belongsTo
*/
public $belongsTo = array(
'Content' => array(
'className' => 'Content',
'foreignKey' => 'object_id',
'conditions' => array(
'Comment.object_id = Content.id',
)
),
);
/**
* @see Model::__construct
*/
public function __construct($id = false, $table = null, $ds = null) {
// check for newsstudio
if (CakePlugin::loaded('NewModel')) {
$this->belongsTo['NewModel'] = array(
'className' => 'NewModel.NewModel',
'foreignKey' => 'object_id',
'conditions' => array(
'Comment.object_id = NewModel.id',
)
);
}
parent::__construct($id, $table, $ds);
}
}