cakephp组件$ this-> controller-> modelClass

时间:2011-05-12 19:49:52

标签: cakephp model components 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()) {
  1. 如何使用$ this-> controller-> modelclass
  2. 如何使用组件中的任何模型
  3. 感谢任何建议

1 个答案:

答案 0 :(得分:14)

默认情况下未定义

$this->controller。您必须手动保存对控制器的引用,例如在组件的initialize()方法中:

public function initialize(&$controller, $settings = array()) {
    $this->controller = $controller;
}

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