Sunday, February 27, 2011

loadModel() and App::import('Model', 'ModelName')

Hey guys, I'm having some trouble telling my controller actions which
model to use. My controller is structed like so:
==================================
class AdminController extends AppController {
var $name = 'Admin';
var $uses = array('News', 'User');

function action_that_uses_news_model() {
}

function action_that_uses_user_model() {
}
==================================

I'd like to ask how is it that I tell action_that_uses_user_model() to
use the User model and not the News model.

I have tried removing $uses (as I see it is to be deprecated) and
insertnig $this->loadModel('ModelName') at the top of my actions, but
it doesn't seem to load the model before the page is rendered so any
data entered via forms aren't being validated (but it does allow me
CRUD functionality - it just doesn't validate my data).

==================================
function action_that_uses_news_model() {
$this->loadModel('News');
}
==================================

I'm not entirely sure how to use the App::import('Model', 'ModelName')
to load a model - I've tried replacing $this->loadModel('ModelName')
with it but it just throws errors [["Call to a member function find()
on a non-object" when I try to use $this->News->find()]], so I assumed
I had to initialise the object, but doing $Model = new Model() gives
me the same error

==================================
function action_that_uses_news_model() {
App::import('Model, 'News');
}
==================================

is there something obvious that I'm doing wrong or can anyone give me
some advice/point me to relevant docs? I thought I could put $this-
>loadModel() inside the beforeFilter() and load the relevant model
depending on what action is being requested, but this doesn't quite
'feel' like the proper way to do things, and I'd rather implement
App::import as it is the suggested way in the docs?

Any advice would be greatly appreciated :)

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: