【问题标题】:how to load or use a Model in a Component File especially in Cakephp 2.0?如何在组件文件中加载或使用模型,尤其是在 Cakephp 2.0 中?
【发布时间】:2012-07-06 07:45:24
【问题描述】:

我需要在组件中加载模型以将相关模型的数据保存在该组件本身中。

我的组件名称是 ImagemuploadComponent

在这个组件中,我想加载 Attachement 模型。 我已经尝试了以下代码,但没有奏效。 (在 cakephp 1.3 中运行良好)

  $Attachment = ClassRegistry::init('Attachment');
  $this->loadModel('Attachment');

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-model


    【解决方案1】:

    试试这个代码。这将导入您的模型并实例化。

    在此之后,您可以作为控制器执行任何操作。

    App::import('Model','Attachment');
    $this->Attachment = &new Attachment();
    $this->Attachment->save($your_data);
    

    【讨论】:

    • 这个答案很糟糕,因为多个原因给出了错误的建议:App::import 不推荐用于加载类,应该使用 App::uses() 并且在这种情况下对于模型来说应该是 ClassRegistry::init('Attachment');问题是针对 2.0 而不是 1.3。
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多