致命错误:无法重新声明类NewsModel [英] Fatal error: Cannot redeclare class NewsModel

查看:99
本文介绍了致命错误:无法重新声明类NewsModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题有点奇怪,自2天以来我一直在与之抗争,在将其发布为错误之前,我只是想确保这不是我的错误。

the problem is a bit strange, I'm fighting with it since 2 days and before I post it as a bug just wanted to make sure this is not my mistake.

我有一个干净的CakePHP副本,有一个已设置的数据库,Cake可以毫无问题地连接到我的数据库。
在我的数据库中,我有一个名为 news的表,并且我有一个带有以下代码的控制器:

I have a clean CakePHP copy, I have a DB which is setup and Cake can connect to my DB with no problem. in my DB I have a table named "news", and I have a controller with the following code:

<?php

class UsersController extends AppController {
    public $uses = array('News');

    public function news(){
        $news = $this->News->find('all');
        var_dump($news);
        die;
    }
}

只要我没有为我创建模型文件表,此查询成功运行,但是一旦我创建以下模型文件(在模型文件夹中另存为News.php),我就会看到代码后出现的错误:

as long as I dont create the model file for my table, this query runs successfully, but as soon as I create the following Model file (save as News.php in model folder) I see the error that comes after code:

<?php

class NewsModel extends AppModel {
}

错误:

Fatal error: Cannot redeclare class NewsModel in C:\...\cakephp\app\Model\News.php on line 4

I 'm在Windows上并运行php> 5.2.8

I'm on the windows and running php > 5.2.8

推荐答案

好,该类应为新闻,而没有模型

Well, the class should be "News", without the model part.

class News extends AppModel { }

文档在各处显示示例 。可能会有混淆,因为新闻控制器被称为 NewsController 和新闻组件被称为 NewsComponent ,但答案是我在此处,这是为了避免名称冲突(阅读后会给您带来片刻的感动) )。

The docs show examples everywhere. There may be confusion because the "News" Controller is called NewsController and the News Component, NewsComponent, but from the answer I got here, it's to avoid name collision (which gives you a do'h moment after you read it).

对于将来的开发,我发现仅烘焙模型/控制器/视图然后删除不需要的内容并更改需要的内容会更容易。

For future development, I find it easier just to bake the models/controllers/views and then delete what I don't need and change what I need changed.

这篇关于致命错误:无法重新声明类NewsModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆