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 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧