How do I use Model in Helper - CakePHP 2.x

10,650

working code should be

//let $modelName be User  
App::import("Model", "User");  
$model = new User();  
$model->find("list");  

I hope this will help some needy fellow

Share:
10,650
Mubasshir Pawle
Author by

Mubasshir Pawle

Updated on June 04, 2022

Comments

  • Mubasshir Pawle
    Mubasshir Pawle almost 2 years

    I recently upgraded my app from cake 1.3.x to cake 2.x. Now I have a helper which uses model in some function. Initially syntax for loading model was (working for 1.3.x)

         App::import('Model', $modelName);
         $modelObject = &ClassRegistry::getObject($modelName);    
         $modelObject->find()
    

    Now I changed it to following

        App::uses($modelName,'Model');
        $modelObject = &ClassRegistry::getObject($modelName);
        $modelObject->find()
    

    Problem is, this conversion is not working. Can anybody tell me where am I doing wrong. Thanking in advance.

    PS: Error message is:
    Call to a member function find() on a non-object