cakephp组件$ this-> controller-> modelClass

kri*_*wat 4 components cakephp model cakephp-1.3

在组件I中尝试访问Myprofile模型

class SignMeupComponent extends Object
   public function register() {
    $this->__isLoggedIn();
    if (!empty($this->controller->data)) {
        extract($this->settings);
        $model = $this->controller->modelClass;
        $this->controller->loadModel($model);
         $this->controller->{$model}->Myprofile->save($this->controller->data);
       $this->controller->data['Myprofile']['user_id'] = $this->controller->{$model}->id;
        $this->controller->{$model}->set($this->controller->data);
            if ($this->controller->{$model}->validates()) {
Run Code Online (Sandbox Code Playgroud)
  1. 如何使用$ this-> controller-> modelclass
  2. 如何在组件中使用任何模型

谢谢你的任何建议

dho*_*tet 14

$this->controller默认情况下未定义.您必须手动保存对控制器的引用,例如在initialize()组件的方法中:

public function initialize(&$controller, $settings = array()) {
    $this->controller = $controller;
}
Run Code Online (Sandbox Code Playgroud)

然后,您应该能够访问控制器的属性和方法.