【问题标题】:CakePHP has the wrong model nameCakePHP 的模型名称错误
【发布时间】:2011-03-27 22:50:37
【问题描述】:

我现在正在试用 CakePHP,但我的应用程序无法运行,因为 CakePHP “认为”我的模型名称是“Tach”,而实际上它是“Tache”。

为什么会这样?

我的控制器定义为: 应用程序/控制器/taches_controller.php

class TachesController extends AppController {

function index() {

    $allTaches = $this->Tache->find('all');

    $this->set('taches', $allTaches);

}

}

这是我的模型: 应用程序/模型/tache.php

class Tache extends AppModel {

var $useTable = 'taches';

}

如果我在控制器中使用“Tache”,我会收到错误:

        $allTaches = $this->Tache->find('all');

但如果我使用“Tach”,我不会收到任何错误:

        $allTaches = $this->Tach->find('all');

为什么我不能使用型号名称“Tache”?难道我做错了什么 ?顺便说一句,我在 php 5.3 上,我的 CakePHP 版本是 1.3.8

谢谢!

亚历克斯

【问题讨论】:

  • 你使用的是什么版本的 CakePHP?
  • 哦,对不起,我忘了。现在是 1.3.8。
  • 您的 Taches 似乎成为某种自动去复数化的受害者。虽然我不知道 CakePHP..

标签: php cakephp model


【解决方案1】:

CakePHP 的默认变形规则认为 Taches 是 Tach 的复数形式。

您需要使用 Inflector 类来添加自定义变形。

请参阅以下内容:

回顾一下,您需要在 app/config/bootstrap.php 文件中添加如下内容:

Inflector::rules('plural', array('irregular' => array('tache' => 'taches')));

【讨论】:

    【解决方案2】:

    您可以在 TachesController 中使用模型名称设置 $uses:

    class TachesController extends AppController {
    
        var $uses = 'Tache';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多