tags:

views:

30

answers:

3

I'm trying to implement a simple way to manage static pages in CakePhp, as described in this article.

The problem I'm facing is that App::import() doesn't seem to import the required class in the routes.php file.

The code is the following:

App::import('Model','StaticPage');
$page = new StaticPage();

$slugs = $page->find('list', array(
        'fields' => array('StaticPage.slug'),
        'order' => 'StaticPage.slug DESC'
));

I'm getting the error: Fatal error: Class 'StaticPage' not found in ...
This class is present in the models folder (models/StaticPage.php).

I just started CakePhp a few weeks ago and I guess I'm missing a simple thing here...

I'm using CakePhp 1.3 and Php 5.2.42.

A: 

Like the error says: You are missing the Class StaticPage. Are you sure that you have this file? If you do, sure that it's in right place, has the right filename so the autoloader can find it?.

kalkin
The file is located in app/models/StaticPage.php and is implemented as in the linked article
Julien Poulin
+1  A: 

Having taken a quick look at the article you reference, your snippet doesn't match up. You're importing the ClassRegistry class (which doesn't need to be imported) and then trying to instantiate a StaticPage. I'd recommend removing the AppImport reference all together and using ClassRegistry:

$page = ClassRegistry::init( 'StaticPage' );

You don't need the AppImport line because ClassRegistry::init() both loads the model and instantiates the object.

The other (potential) problem I see is that your model file name doesn't follow convention. It should be models/static_page.php. Cake's inflection may not be handling the deviation from the norm.

Rob Wilkerson
Looks like you edited your snippet at the same time I posted my answer. Now the snippets match up.
Rob Wilkerson
+1  A: 

I think the reason it doesn't work is because you don't follow CakePHP's naming conventions for file names: file names are lowercase and underscored. So renaming your file to static_page.php should fix the problem.

dhofstet