CakePHP 模型名称使用 PHP 保留字

发布于 2024-08-15 05:33:58 字数 541 浏览 2 评论 0原文

嘿,我已经为很多事情编写了 CakePHP 代码,但令人惊讶的是,之前从未遇到过这个问题。我还彻底搜索了网络和 CakePHP 文档,但没有找到我的问题的答案。我的问题是,我的模型有一个表,应命名为 Class,显然我不能使用该名称,因为它是保留的 PHP 关键字。我必须有哪些选项才能正确引用该模型。

到目前为止我已经;

  1. 将我的类模型文件重命名为player_class.php
  2. 将我的类模型类重命名为 PlayerClass
  3. 将 var $name 更改为 'PlayerClass'
  4. 添加到我的类模型类; var $useTable = '类';
  5. 将我的类控制器重命名为player_classes_controller.php
  6. 将我的类控制器类重命名为 PlayerClassesController
  7. 将 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;

  1. Renamed my class model file to player_class.php
  2. Renamed my class model class to PlayerClass
  3. Changed var $name to 'PlayerClass'
  4. Added to my class model class; var $useTable = 'classes';
  5. Renamed my class controller to player_classes_controller.php
  6. Renamed my class controller class to PlayerClassesController
  7. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-08-22 05:33:58

曾经测试了 Cake 1.2 的所有 CakePHP 类名称,如果它们可以用作模型名称,结果如下:

不可能是:

app
appcontroller
appmodel
behaviorcollection
cache
cacheengine
cakelog
cakesession
classregistry
component
configure
connectionmanager
controller
datasource
debugger
dispatcher
file
fileengine
folder
helper
inflector
model
modelbehavior
object
overloadable
overloadable2
router
security
sessioncomponent
set
string
validation

可能是:

acl
aclbase
aclbehavior
aclcomponent
aclnode
aclshell
aco
acoaction
admin
ajaxhelper
apcengine
apishell
app_model
apphelper
aro
authcomponent
bake
baker
bakeshell
behavior
cachehelper
cake
cakeschema
cakesocket
consoleshell
containablebehavior
controllertask
cookiecomponent
dbacl
dbaclschema
dbconfigtask
dboadodb
dbodb2
dbofirebird
dbomssql
dbomysql
dbomysqlbase
dbomysqli
dboodbc
dbooracle
dbopostgres
dbosource
dbosqlite
dbosybase
element
emailcomponent
error
errorhandler
extracttask
flay
formhelper
htmlhelper
httpsocket
i18n
i18nmodel
i18nschema
i18nshell
iniacl
javascripthelper
jshelper
jshelperobject
l10n
layout
magicdb
magicfileresource
mediaview
memcacheengine
modeltask
multibyte
numberhelper
page
pagescontroller
paginatorhelper
permission
plugintask
projecttask
requesthandlercomponent
rsshelper
sanitize
scaffold
schema
schemashell
securitycomponent
sessionhelper
sessionsschema
shell
shelldispatcher
test
testsuiteshell
testtask
texthelper
themeview
timehelper
translate
translatebehavior
treebehavior
viewtask
xcacheengine
xml
xmlelement
xmlhelper
xmlmanager
xmlnode
xmltextnode

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:

app
appcontroller
appmodel
behaviorcollection
cache
cacheengine
cakelog
cakesession
classregistry
component
configure
connectionmanager
controller
datasource
debugger
dispatcher
file
fileengine
folder
helper
inflector
model
modelbehavior
object
overloadable
overloadable2
router
security
sessioncomponent
set
string
validation

Possible is:

acl
aclbase
aclbehavior
aclcomponent
aclnode
aclshell
aco
acoaction
admin
ajaxhelper
apcengine
apishell
app_model
apphelper
aro
authcomponent
bake
baker
bakeshell
behavior
cachehelper
cake
cakeschema
cakesocket
consoleshell
containablebehavior
controllertask
cookiecomponent
dbacl
dbaclschema
dbconfigtask
dboadodb
dbodb2
dbofirebird
dbomssql
dbomysql
dbomysqlbase
dbomysqli
dboodbc
dbooracle
dbopostgres
dbosource
dbosqlite
dbosybase
element
emailcomponent
error
errorhandler
extracttask
flay
formhelper
htmlhelper
httpsocket
i18n
i18nmodel
i18nschema
i18nshell
iniacl
javascripthelper
jshelper
jshelperobject
l10n
layout
magicdb
magicfileresource
mediaview
memcacheengine
modeltask
multibyte
numberhelper
page
pagescontroller
paginatorhelper
permission
plugintask
projecttask
requesthandlercomponent
rsshelper
sanitize
scaffold
schema
schemashell
securitycomponent
sessionhelper
sessionsschema
shell
shelldispatcher
test
testsuiteshell
testtask
texthelper
themeview
timehelper
translate
translatebehavior
treebehavior
viewtask
xcacheengine
xml
xmlelement
xmlhelper
xmlmanager
xmlnode
xmltextnode
稚然 2024-08-22 05:33:58

当我遇到这类问题时,我通常会做你所做的事情,只是我在保留字前加上“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

明媚如初 2024-08-22 05:33:58

我可以支持这个解决方案。我遇到了同样的问题,并使用了客户姓名首字母缩写的前缀。最后打电话给我的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文