CakePHP의 구성 요소에서 모델을 사용하려면 어떻게해야합니까?구성 요소에서 모델 사용
컨트롤러에서당신은
public $uses = array(...);
를 사용할 수 있지만 그 구성 요소에서 작동하지 않습니다.
무엇이 있습니까?
$result= $model->find('all');
CakePHP의 구성 요소에서 모델을 사용하려면 어떻게해야합니까?구성 요소에서 모델 사용
컨트롤러에서당신은
public $uses = array(...);
를 사용할 수 있지만 그 구성 요소에서 작동하지 않습니다.
무엇이 있습니까?
$result= $model->find('all');
이 코드를 시도
$this->ModelName = ClassRegistry::init('ModelName');
을하지만 돈을 생각한다 모델 내부에서 모델을 사용하지 마십시오.
현재 모델이 필요한 경우 구성 요소의 initialize() 또는 startup() 콜백을 사용할 수 있습니다.
public function initialize(Controller $controller) {
$this->Controller = $controller;
$this->Model = $this->Controller->{$this->Controller->modelClass};
$this->modelAlias = $this->Model->alias;
parent::initialize($controller);
}
이제 구성 요소의 모든 곳에서 모델에 액세스 할 수 있습니다.
public function countAllItems() {
return $this->Model->find('count');
}
이 작동, 당신은 welocome이 – AgeDeO
감사합니다 : 당신이 내 목숨을 구해 D : D –
더 나은'Yourmodel' (대문자) – mark