【问题标题】:Load model or $uses array which need to be used ? while we accessing other models加载需要使用的模型或 $uses 数组?当我们访问其他模型时
【发布时间】:2011-10-22 15:36:16
【问题描述】:

我有一个用户控制器,我需要在该用户控制器上使用照片模型 我可以使用以下哪个来访问该模型,哪个是标准使用?

$this->loadModel('Photo');

或

var $uses =array('User','Photo');

加载需要使用的模型或$uses数组?

【问题讨论】:

    标签: cakephp cakephp-1.3 cakephp-model


    【解决方案1】:

    正如 Anh 所说,最好通过关系访问模型:$this->User->Photo->whatever()。如果模型不相关,请使用$uses 或loadModel()。

    $uses 中的模型在每次使用控制器时都会加载,因此最好仅在整个控制器需要其他模型时才使用它。如果你只是随机需要它,那么loadModel() 更好。

    【讨论】:

    • 它们之间没有任何关系
    【解决方案2】:

    标准是让Photo 和User 模型相互关联(直接或间接):也许

    Photo belongsTo Album belongsTo User.
    

    loadModel 或 uses:只使用对您更方便的那个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 2016-01-02
      • 2015-06-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多