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.

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;
    }
}

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 {
}

the error:

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

I'm on the windows and running php > 5.2.8

mohamnag
  • 2,709
  • 5
  • 27
  • 40

3 Answers3

6

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

class News extends AppModel { }

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.

Community
  • 1
  • 1
Nunser
  • 4,512
  • 8
  • 25
  • 37
0

try using var $useTable = 'news'; in your model.. Hopefully your problem will solve.

Gaurav Chauriya
  • 304
  • 2
  • 12
  • the problem was obviously something else, but however just to give a hint, I had already tried that and this made no difference ;) – mohamnag Oct 23 '13 at 14:58
-1

Model should be singular to better fit cake convetions

<?php
App::uses('AppModel', 'Model');
class New extends AppModel {

}
?>

$news = $this->New->find('all');
evansLY
  • 122
  • 3
  • 3
    but "new" is not singular of "news" ([forum reference](http://forum.wordreference.com/showthread.php?t=1067564)) :S – Nunser Oct 16 '13 at 15:40
  • 3
    Plus "new" is a reserved keyword in PHP and will lead to a parse error when used as a class name. – dhofstet Oct 17 '13 at 05:04