CakePHP模型名称使用PHP保留字 [英] CakePHP Model Name Uses PHP Reserved Word
问题描述
嘿,我已经编码CakePHP的一些事情,但从来没有遇到过这个问题之前令人惊讶。还有我彻底搜索网和CakePHP文档,没有找到我的问题的答案。我的问题是,我有一个表为我的模型,应该命名为Class,显然我不能使用那个名称,因为它是一个保留的PHP关键字。
Hey I have coded CakePHP for a number of things but never ran into this problem before surprisingly. Also I have thoroughly searched the net and CakePHP docs and have not found an answer to my question. My question is, I have a table for my model that should be named Class, obviously I cannot use that name though since it's a reserved PHP keyword. What options do I have to be able to refer to this model appropriately.
到目前为止,我有
- 将我的类模型文件重命名为player_class.php
- 将我的类模型类重命名为PlayerClass
- 将var $ name更改为'PlayerClass'
- 添加到我的类模型类中; var $ useTable ='classes';
- 将我的类控制器重命名为player_classes_controller.php
- 将我的类控制器类重命名为PlayerClassesController
- 将var $ name更改为PlayerClasses
- Renamed my class model file to player_class.php
- Renamed my class model class to PlayerClass
- Changed var $name to 'PlayerClass'
- Added to my class model class; var $useTable = 'classes';
- Renamed my class controller to player_classes_controller.php
- Renamed my class controller class to PlayerClassesController
- Changed var $name to 'PlayerClasses'
这样做有效,对于其他选项,能够将它引用为类静态,像我可以做任何类型的修改像_Class?
While this does work, is this what has to be done or are to other options to be able to refer to it as Class still, like can I do any sort of mangling like _Class?
推荐答案
当我遇到这种问题,我通常做你做的,只有我保留字的前缀我的(所以当我读取代码,它看起来不像那个类与播放器有关。例如,只是在另一天我想模拟一个ACO模型..但已经存在的蛋糕(保留字的相同场景)所以我创建了一个名为Myaco的模型。
When i run into this sort of problem i usually do what you did, only i prefix the reserved word with "My" (so when i read the code it doesn't look like that class has anything to do with "Player"... for example, just the other day i wanted to model a "ACO" model.. but that already existed in cake (same scenario of reserved word) so i created a model called Myaco.
我认为你应该把它命名为Myclass。
I think you should just name it Myclass.
关于模型名称和控制器名称更改 - 我认为你做得很好, 。您唯一真正的选择是使用 $ useTable ='classed';
来使用您的数据库表。
Regarding the model name and controller name changes- i think you did good, i would do the same. Your only real option is to use the $useTable = 'classed';
to use your DB table.
你使用下划线前缀,我相信蛋糕将无法处理它(它将在Inflector类中失败)。
If you use the underscore prefix, i believe cake will not be able to handle it (it will fail in the Inflector class).
祝你好运
这篇关于CakePHP模型名称使用PHP保留字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!