Question

I'm still trying to migrate from CakePHP 1.3 to 2.0

I have a Controller UsersController and its Model User. The class User has some constants which I could easily access from the UsersController using User::constant. But for CakePHP 2.0 it doesn't work: I get an error saying the User class is not found. It works if I App::Import('Model', 'User');.

Was it helpful?

Solution

It sure has to do with their built-in lazy loading in 2.0! you simple need to tell this file that it has other dependencies

do that at the very top of your UserController file:

<?php
App::uses('User', 'Model');

then everything works fine

OTHER TIPS

You can try setting the controller name (in UsersController):

var $name = 'Users';

Or using the "uses" var (in other controllers:

public $uses = array('User');

Does that not work?

Did you declare the name of the model like that ?

class User extends AppModel {
    public $name = 'User';
}

Do you have others variables in your UsersController ?

I set variables in the model like this:

//Person model
public $genders = array('m' => 'male', 'f' => 'female');

Then get them from the controller like this:

//People controller
$genders = $this->Person->genders;

No special code needed. (Is this what you're talking about?)

Also, I don't believe you need to set the $name variable anywhere anymore - I think that was just for PHP 4. (not 100% on that part, but... I don't ever set that anymore, and my Cake2 apps run fine)

You have to load the model like this:

$this->loadModel('User');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top