cakephp中table、模型文件和模型class的命名约定

naming convention of table, model file and model class in cakephp

我正在使用 cakephp 2.6.9。我有一个名为 table 的 chat_info 和模型文件:ChatInfo.php 和 class inside ChatInfo:

 <?php
/**
* 
*/
class ChatInfo extends AppModel
{
    var $name = "chatinfo";

}

?>

但是显示错误。我搜索了这个错误,发现这是由于 cakecaphp 中的命名约定违规造成的。但是我在这里做错了什么

如果您将 table 从 chat_info 重命名为 chat_infos 它将没事

或者,如果您想为 table 保留该名称,则将其添加到您的模型中:

$useTable = 'chat_info';

并且您的模型将关联到 table chat_info(没有复数模式)

模型::useTable

the docs 中发现:

The useTable property specifies the database table name. By default, the model uses the lowercase, plural form of the model’s class name

惯例并不是要成为牢不可破的规则。它们是指导方针,如果遵循这些指导方针,生活会更轻松。这并不意味着必须遵循它们。使用 useTable 可以使用任何 table 名称,在这种情况下:

class ChatInfo extends AppModel
{
    public $useTable = "chat_info";

}

两个旁白,假设你实际上没有使用 php4:

  1. 将模型名称设置为不同于实际模型名称的东西是一个糟糕的想法。没有必要将其设置为任何值,因为它的设计目的是 php4 兼容性。模型的名称是 the class name,将其设置为其他名称很容易导致混淆或意外的副作用。
  2. var声明变量是php4风格,使用php版本的特性,即用public, protected or private.[=30声明变量=]