php 在默认数据源中找不到模型的 cakephp 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13511136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:36:31  来源:igfitidea点击:

cakephp Table for model was not found in datasource default

phpmysqlcakephp

提问by user1511579

I have just a table on my database named "ficha_seg". The name of my model file is "Ficha.php" and the name of the controller is "FichasController.php".

我的数据库中只有一个名为“ficha_seg”的表。我的模型文件的名称是“Ficha.php”,控制器的名称是“FichasController.php”。

So, why i'm getting the error:

那么,为什么我收到错误:

Error: Table fichas for model Ficha was not found in datasource default.

after configured my index() method of controller like this:

像这样配置我的控制器的 index() 方法后:

 public function index() {
    $this->set('ficha_seg', $this->Ficha->find('all'));
}

回答by nIcO

By default, the model uses the lowercase, plural form of the model's class name for the database table name.

默认情况下,模型使用模型类名的小写复数形式作为数据库表名。

If you need to use another table name for your model, you can use the useTableattribute:

如果您需要为您的模型使用另一个表名,您可以使用该useTable属性:

class Ficha extends AppModel 
{
    public $useTable = 'ficha_seg';
}

See http://book.cakephp.org/2.0/en/models/model-attributes.html#usetableand Model conventionsin the Cookbook

请参阅食谱中的http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable模型约定

回答by Alvaro

To follow CakePHP conventions your table name should be in plural: ficha_segsand your model name should be 'FichaSeg'.

要遵循 CakePHP 约定,您的表名应该是复数形式:ficha_segs并且您的模型名称应该是“FichaSeg”。

If you don't want to follow it for any reason, do what @nlcO says.

如果您出于任何原因不想遵循它,请按照@nlcO 说的去做。