CakePHP 模型名称使用 PHP 保留字
嘿,我已经为很多事情编写了 CakePHP 代码,但令人惊讶的是,之前从未遇到过这个问题。我还彻底搜索了网络和 CakePHP 文档,但没有找到我的问题的答案。我的问题是,我的模型有一个表,应命名为 Class,显然我不能使用该名称,因为它是保留的 PHP 关键字。我必须有哪些选项才能正确引用该模型。
到目前为止我已经;
- 将我的类模型文件重命名为player_class.php
- 将我的类模型类重命名为 PlayerClass
- 将 var $name 更改为 'PlayerClass'
- 添加到我的类模型类; var $useTable = '类';
- 将我的类控制器重命名为player_classes_controller.php
- 将我的类控制器类重命名为 PlayerClassesController
- 将 var $name 更改为 'PlayerClasses'
虽然这确实有效,但这是必须做的还是其他选项才能将其引用为类,就像我可以做任何类型的修改,比如 _Class 吗?
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.
So far I have;
- 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'
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我 曾经测试了 Cake 1.2 的所有 CakePHP 类名称,如果它们可以用作模型名称,结果如下:
不可能是:
可能是:
I once tested all CakePHP class names for Cake 1.2 if they can be used as Model names, here are the results:
NOT possible is:
Possible is:
当我遇到这类问题时,我通常会做你所做的事情,只是我在保留字前加上“My”前缀(所以当我阅读代码时,它看起来不像该类与“Player”有任何关系。 ,就在前几天,我想建模一个“ACO”模型。但它已经存在于 cake 中(保留字的情况相同),所以我创建了一个名为 Myaco 的模型
。
例如 ,你唯一真正的选择是使用
$useTable = 'classed';
来使用你的数据库表。模型名称和控制器名称更改 - 我认为你做得很好,我会做同样的事情 使用下划线前缀,我相信 cake 将无法处理它(它会在 Inflector 类中失败)
祝你好运。
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.
I think you should just name it Myclass.
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.If you use the underscore prefix, i believe cake will not be able to handle it (it will fail in the Inflector class).
Good luck
我可以支持这个解决方案。我遇到了同样的问题,并使用了客户姓名首字母缩写的前缀。最后打电话给我的 Dtclass。不幸的是,我花了一个小时左右才弄清楚问题出在哪里。其中一种情况是,答案一直盯着你,直到你最终认出它。
I can second that solution. I had the same problem and used a prefix that was the initials of the client. Ended up calling mine Dtclass. Unfortunately, it took me an hour or so to figure out what the problem was. One of those cases where the answer stares you in the face all the time till you finally recognize it.