dsl36367 2013-10-14 16:57
浏览 225
已采纳

理解'$ this-> model'

I am a cakePHP newbie, who has just been assigned to work on an existing system built with cakePHP 1.3. As I get familiar with the project and it's contents their are snippets of code I don't quite understand. Again and again (usually in a controller file) I see these:

'$this->model'

'$this->Model'

'$this->{$this->model}'

Searching google, the cake docs, and stackoverflow has yet to reveal a concise explanation of what exactly these strings are referring to. Many of the controllers I am working with have more than one model with which they interact, how do I know which one these snippets are invoking?

For instance web_forms_controller.php has 3 models which go with it web_forms.php, web_forms_field.php, and web_forms_submission.php. I feel like I am missing something basic here, but RTM has yet to reveal the answer I am after.

So in the above instance I am guessing that '$this->model' and '$this->Model' seen in web_forms_controller.php refer to the web_forms.php model. What '$this->{$this->model}' is a reference to in this case I have no idea.

  • 写回答

1条回答 默认 最新

  • duanhuanyou6478 2013-10-15 05:56
    关注

    First off CakePHP uses the "convention over configuration" convention. This heavily explains a lot! :) There is a thingy called Inflector - it "just" 'pluralizes and singularizes English nouns. Used by Cake's naming conventions throughout the framework.'

    This combined with the conventions and a few key principles makes it possible that Cake tells which Class to bind to which other.

    So, $this->model in a controller will be the Model directly associated with this Controller. For example in a UsersController, $this->model would be the User model.

    Other binded models are defined in the $uses property of the Controller. Through $this->model you call only the controller's own model. To call another binded model (for ex Post) use:

    $this->Post->findAll();
    

    $this->model i just a convenient shortcut from any given controller.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab