views:

385

answers:

2

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?

Thanks for the help everyone.
--Alex

A: 

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

Ken
Thanks for the reply, that's a good idea. My class, "Class", is for a world of warcraft class like warrior, so I may stick with PlayerClass in this instance, I will very likely use that convention in other such cases.
Axpen
Jakob
Good observation, that would be the better choice.
Axpen
A: 

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.

Dan Berlyoung