1

I was planning my database and since CakePHP hasn't named its classes with its own prefixes, there are many classes that may collison with model classes created according to naming conventions.

So my question is in three parts:

  1. Is there a list of database table names that are reserved or a simple way to check if it is? It would be a pain if I plan database with 100 tables and notice some of the tables and their connections would have to be renamed...

  2. Is there a simple way around it without breaking any CakePHP magic? Is it adviced? Would naming the tables in other way by adding own prefix 'my_' or similar at the beginning of table name the best way?

  3. Is namespaces or something similar coming to CakePHP version 2 that would allow use of all kinds of table names?

3 Answers 3

1
  1. No, there aren't. Cake doesn't care what you name your tables as long as you adhere to Cake's naming conventions. It generates the schemas it uses for magic model methods the first time a model/s is loaded by a controller; you don't have to lift a finger. See http://book.cakephp.org/view/903/Model-and-Database-Conventions

  2. Best advice: don't fight Cake on this. If you really cannot adhere to to Cake's conventions, you might as well not use Cake; it's stupidly difficult, confusing and succeeding just means you've lost most of Cake's heavy lifting abilities. Pluralizing your table names isn't THAT bad, and Cake will be happy.

  3. This functionality is already available in 1.3 - name your tables anything that pleases you (as long as they're plural words.)

-- You'd probably be well-served to check out baking apps in the console so you can get familiar with what Cake wants to see and how it works on different table layouts.


Edit after clarification:

Your models, controllers, and view directories all share a common name, like so:

 // in /app/models/rate.php
class Rate extends AppModel {

     var $name = 'Rate';


// in /app/controllers/rates_controller.php -- note the underscore
class RatesController extends AppController {

    // controllers are capitalized + plural
    var $name = 'Rates';

// in /app/views/rates/*.ctp - Cake's magic autoRender expects to find a view file with // the same name as the action rendering it - the view for Rates::index() is index.ctp

All of your models extend cake's AppModel class (which extends cake's Model class), no prefix needed. All controllers will extend Cake's AppController class - the class name is suffixed with Controller, and the file name is suffixed with _Controller.

You'll fine AppModel and AppController in /app, and they exist expressly for whatever app-wide custom methods / properties you may have. Since all of your models / controllers extend them, inheritance automatically disperses whatever properties / methods you place in them - for example, Auth. ^_^

But you can still name a table Models, or Controllers, or Views, or whatever, I guess. The $name property is an alias; you can create multiple instances of the same table in the same model by aliasing it with a different name. You can create models without tables, and you can switch between multiple tables - or databases, or servers - in a single model. You can also create non-database-type data objects (such as flat xml files) for your models. Dynamically named classes / methods ($$Model::save(), etc) are what's running under the hood anyway. I've done something similar in iterations for the sake of DRYing up my app and I didn't have a problem. (Although I personally doubt actually pulling off a local model named Model would be worth the effort you'd put into the experiment...)

And on that note, Cake's API spells out all it's classes their methods, etc. (generates off the comments in the codebase):

http://api13.cakephp.org/classes

HTH. :D

3
  • The question was about reserved table names such as "models" and "objects" tables which models would become "Model" and "Object" by CakePHPs naming conventions and cake already uses these classes. If I would use for example $this->Model->something it would not work.
    – Pehmolelu
    Jun 11, 2011 at 17:12
  • Yeh, I should have said that I'm well aware how the actual conventions and cake works. You would not had to spell it for me, but thanks anyways ;) The last sentence however was answer to my first question, thank you :)
    – Pehmolelu
    Jun 11, 2011 at 18:29
  • I should have been more detail on the second question, I didn't mean I want to break the conventions, I just wondered if there is some way that doesn't. I have a table objects and I cant use it just like that because there exists that Object class already. So my question is how I get it to work if I would really have to have the objects table in my database. And if there isn't good way, would the best way to solve it be adding prefix at the beginning like: my_objects.
    – Pehmolelu
    Jun 11, 2011 at 18:29
1

I know from experience you can't use table names like 'files' and 'models' because they create classes that are already used by Cake for other things such as File and Model.

I haven't come across any other problems like this but I'm sure they are there to be found.

I would suggest avoiding the use of any name used by cake core classes.

2
  • I just got whacked on this (Cake 2.2). I've been using a web app with a model entitled "File" without a problem for over a year. Now I am finally using Cake's schema system and I am getting a weird "Error: Table 1 for model File was not found in datasource" which I'm almost sure is because of a naming collision with a core class. Cake never gave an error until now! Jan 8, 2015 at 20:17
  • Yeah, I did a while ago. It's a pain but you'll be able to do it in 3.0 I think, using namespaces.
    – 93196.93
    Jan 20, 2015 at 22:25
1

I know this is a bit of an old thread but just came across it searching for something else. I think this shoud help answer question #2. In database.php you can add your db table prefix in the DATABASE_CONFIG class /app/config/database.php. See the last key in the config array below:

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'database_name',
    'prefix' => '',
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.