Ezequiel
Ezequiel

Reputation: 718

Accessing userModel in CakePhp 2.2.3

I'm in the process of upgrading from Cake 1.3 to the latest Cake 2.

I used to access the user model from the App Component as:

$this->Auth->userModel->

However, that field is not there anymore.

Is there an alternative?

I also tried

App::uses('User', 'Model');
$user = new User();

but I get the error

Class 'AppModel' not found

Any alternative way of accessing the user model?

Upvotes: 0

Views: 183

Answers (1)

Borislav Sabev
Borislav Sabev

Reputation: 4856

What exactly do you want to do by accessing the User (or any) model?

If in a Controller you can add the Model with the controller's $uses variable:

public $uses = array('User');

You can also access any Model by "jumping over the Model associations". For example if you're in the UsersController and you wish to use the Costume model where User hasMany Costume and Costume is not in the Controller's $uses property, you can do:

$this->User->Costume->find();

The existence of a Model Association is mandatory otherwise it will not work. This is possible since the User model has an internal association to any of it's associated Models.

Since Components wrap Controllers you should be able to do this also there or there should be a reference to the Controller from where you could call: User->Costume->find(); But in the Component you're better of passing data to it from the Controller than trying to call a model from it.

Upvotes: 1

Related Questions