如何在组件文件中加载或使用模型,尤其是在Cakephp 2.0中?
我需要在组件中加载一个模型,以将相关模型的数据保存在该组件本身中 我的组件名称是ImagemuploadComponent 在这个组件中,我想加载附件模型。 我尝试了下面的代码,但没有成功。(在cakephp 1.3中运行良好)如何在组件文件中加载或使用模型,尤其是在Cakephp 2.0中?,cakephp,cakephp-2.0,cakephp-model,Cakephp,Cakephp 2.0,Cakephp Model,我需要在组件中加载一个模型,以将相关模型的数据保存在该组件本身中 我的组件名称是ImagemuploadComponent 在这个组件中,我想加载附件模型。 我尝试了下面的代码,但没有成功。(在cakephp 1.3中运行良好) 试试这个代码。这将导入您的模型并实例化 在此之后,您可以作为控制器执行任何操作 App::import('Model','Attachment'); $this->Attachment = &new Attachment(); $this->Atta
试试这个代码。这将导入您的模型并实例化 在此之后,您可以作为控制器执行任何操作
App::import('Model','Attachment');
$this->Attachment = &new Attachment();
$this->Attachment->save($your_data);
这个答案是错误的,给出错误建议的原因不止一个:App::import不推荐用于加载类,应该使用App::uses(),对于模型,在这种情况下应该是ClassRegistry::init('Attachment');问题是2.0而不是1.3。
App::import('Model','Attachment');
$this->Attachment = &new Attachment();
$this->Attachment->save($your_data);