Question

I am trying to load another model in one default model. I have a profiles table in my database. but getting error.

User model

App::uses('AuthComponent', 'Controller/Component');   
    class User extends AppModel {

          public function getProfileData(){
                $this->loadModel('Profile');
                $profileData = $this->Profile->findById('8');
                print_r($profileData);
            }
    }

Error :

 Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'loadModel' at line 1

SQL Query: loadModel 
Was it helpful?

Solution

loadmodel works in controllers.

To use a new model in the current one, try:

App::uses('Profile', 'Model');
$Profile = new Profile();
$profileData = $Profile->findById('8');

OTHER TIPS

There is another way.

$profile= ClassRegistry::init('Profile');
$profileData = $profile->findById(8);

Perhaps the Profile model is associated with the User model already. If that's the case, you can access it using:

$this->User->Profile->findById(8);

without the need to load anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top