加载模型或 $uses 数组需要使用哪些?当我们访问其他模型时
我有一个用户控制器,我需要在该 users_controllers 上使用照片模型 我可以使用哪个来访问以下模型,哪个是使用标准?
$this->loadModel('Photo');
或
var $uses =array('User','Photo');
加载需要使用的模型或 $uses 数组?
I have a users controllers, I need to use photos model on that users_controllers
which can i use to access that model from the following and which is standard to use?
$this->loadModel('Photo');
or
var $uses =array('User','Photo');
Load model or $uses array which need to be used ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像 Anh 所说,最好通过关系访问模型:
$this->User->Photo->whatever()
。如果模型不相关,请使用$uses
或loadModel()
。每次使用控制器时都会加载
$uses
中的模型,因此最好仅在整个控制器需要其他模型时才使用它。如果您只是随机需要它,那么loadModel()
更好。Like Anh said, it's best to access the model through relations:
$this->User->Photo->whatever()
. If the models aren't related, use$uses
orloadModel()
.The models in
$uses
are loaded every time the controller is used so it's best to use it only when the other model is needed throughout the controller. If you only need it randomly thenloadModel()
is better.标准是让
Photo
和User
模型相互关联(直接或间接):可能是loadModel 或 uses : 就用你觉得更方便的那个吧。
The standard is having
Photo
andUser
model relate to each other (directly or indirectly): maybeloadModel or uses: just use the one that is more convenient to you.