CakePHP 中保留的数据库表名称是什么?

发布于 2024-11-15 02:59:10 字数 358 浏览 4 评论 0原文

我正在规划我的数据库,并且由于 CakePHP 没有使用自己的前缀命名其类,因此许多类可能与根据命名约定创建的模型类发生冲突。

所以我的问题分为三个部分:

  1. 是否有数据库列表 保留的表名或 简单的方法来检查是否是?它 如果我计划数据库会很痛苦 有 100 张桌子,请注意其中一些 表及其连接将 必须重命名...

  2. 有没有一种简单的方法可以解决这个问题而不破坏任何 CakePHP 的魔力?是否建议?通过在表名开头添加自己的前缀“my_”或类似的前缀来以其他方式命名表是否是最好的方法?

  3. 命名空间或类似的东西会出现吗? CakePHP 版本 2 允许 使用各种表名?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

孤星 2024-11-22 02:59:10
  1. 不,没有。只要您遵守 Cake 的命名约定,Cake 并不关心您如何命名表。当控制器第一次加载模型时,它会生成用于魔术模型方法的模式;您无需动一根手指。请参阅http://book.cakephp.org/view/903/Model- and-Database-Conventions

  2. 最好的建议:不要在这个问题上与 Cake 争论。如果你实在无法遵守Cake的约定,那你还不如不要使用Cake;这是非常困难、令人困惑的,成功只会意味着你失去了蛋糕的大部分举重能力。复数表名并没有那么糟糕,Cake 会很高兴。

  3. 此功能已在 1.3 中提供 - 将您的表格命名为您喜欢的任何名称(只要它们是复数单词。)

-- 您可能会在控制台中查看烘焙应用程序,这样您就可以熟悉 Cake 想要看到的内容以及它在不同表格布局上的工作方式。


澄清后编辑:

您的模型、控制器和视图目录都共享一个通用名称,如下所示:

 // 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 的神奇 autoRender 期望找到一个视图文件
// 与呈现它的操作名称相同 - Rates::index() 的视图是index.ctp

所有模型都扩展 cake 的 AppModel 类(扩展 cake 的 Model 类),不需要前缀。所有控制器都会扩展Cake的AppController类——类名以Controller为后缀,文件名以_Controller为后缀。

您可以在 /app 中设置 AppModel 和 AppController,它们明确存在于您可能拥有的任何应用程序范围的自定义方法/属性中。由于所有模型/控制器都扩展了它们,因此继承会自动分散您在其中放置的任何属性/方法 - 例如,Auth。 ^_^

但我猜你仍然可以将表命名为“模型”、“控制器”、“视图”或其他任何名称。 $name 属性是一个别名;您可以通过使用不同的名称作为别名,在同一模型中创建同一表的多个实例。您可以创建没有表的模型,并且可以在单个模型中的多个表(或数据库或服务器)之间切换。您还可以为模型创建非数据库类型的数据对象(例如平面 xml 文件)。无论如何,动态命名的类/方法($$Model::save() 等)都是在幕后运行的。为了干燥我的应用程序,我在迭代中做了类似的事情,但没有遇到问题。 (尽管我个人怀疑实际上建立一个名为 Model 的本地模型是否值得您在实验中投入的精力......)

并且在这一点上,Cake 的 API 详细说明了它的所有类和方法等(生成代码库中的注释):

http://api13.cakephp.org/classes

HTH。 :D

  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

月朦胧 2024-11-22 02:59:10

根据经验,我知道您不能使用“文件”和“模型”等表名称,因为它们创建的类已被 Cake 用于其他内容(例如文件和模型)。

我还没有遇到过任何其他类似的问题,但我确信它们是可以找到的。

我建议避免使用蛋糕核心类使用的任何名称。

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.

清君侧 2024-11-22 02:59:10

我知道这是一个有点旧的线程,但只是在寻找其他东西时遇到它。我认为这应该有助于回答问题#2。在database.php中,您可以在DATABASE_CONFIG类/app/config/database.php中添加数据库表前缀。请参阅下面的配置数组中的最后一个键:

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

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