0

I am few months old with CakePHP. This is first time I am trying CakePhp Association. I assume I am following almost all instruction but still my model doesn't seem to work.

This is simple 'User' and 'Profile' Model. Table Structure: User:
- id (Primary Key)
- name

Profile:
- id (primary key)
- role
- user_id (Reference Key)

Models: User:

class UserModel extends AppModel{
    var $name = 'User';
    public $hasOne = array(
        'Profile'
    );

}

Profile:

class ProfileModel extends AppModel{
    var $name = 'Profile';
    public $belongsTo = array('User'); } 

Controller: Users:

lass UsersController extends AppController{
    var $name = 'Users';
    public $scaffold;

    function index(){
        $user = $this->User->find('all'); //HERE I expect to get User and Profiles data
        pr($user);
    }

} 

Profiles:

 class ProfilesController extends AppController{
    var $name = 'Profiles';
    public $scaffold;

    function index(){
        $profile = $this->Profile->find('all');
        pr($profile);
    }

} 

If I run users: /localhost/test_php_apps/users/ I get:

Array (
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [name] => rohini
                )

        )

) 

I am wondering why 'Profile' data is not shown. I have manually added records in tables.

Further if I try in UsersController: $user = $this->User->Profile->find('all'); I get the following error:

Call to a member function find() on a non-object

My guess is something is wrong with setting up Associations. But not sure what is messing things up.

I know this is very basic question, but even after reading 10 to 15 related cases I don't seem to find the answer.

Any help will be much appreciated.

Nunser
  • 4,512
  • 8
  • 25
  • 37
Bilal Wani
  • 33
  • 4
  • I don't see any containable action going on here. What is you `$recursive` value? – Nunser Feb 20 '14 at 19:58
  • I have set it:class AppModel extends Model { public $actAs = 'Containable'; } – Bilal Wani Feb 20 '14 at 19:59
  • don't use Containable. this is pretty simple. when you get "Call to a member function find() on a non-object" means your model is not associated. Containable used for other purpose – Fury Feb 21 '14 at 03:29

3 Answers3

0

Do me a favor, change

class UserModel extends AppModel

to

class User extends AppModel

and

class ProfileModel extends AppModel

to

class Profile extends AppModel

and see if that helps when doing $user = $this->User->Profile->find('all'); in the UsersController.

Also, is

public $actsAs = array('Containable');

not

public $actAs = 'Containable';

not actAs. See if that helps any. If not, it would be helpful to know your cake version.

See here for containable and this for naming model conventions.

Community
  • 1
  • 1
Nunser
  • 4,512
  • 8
  • 25
  • 37
0

add the following lines in your usermodel

public $useTable = 'YOUR USERTABLENAME';

and change

public $hasOne = array(
        'Profile'
    );

to public $hasOne = 'Profile';

in your profilmodel add the same.

public $useTable = 'YOUR PROFILETABLENAME';

and change

public $belongsTo = array('User'); 

to

public $belongsTo = array('User' => array('className' => 'User',
                                          'foreignKey' => 'user_id'));

in your userscontontroller add this

public $uses = array('User','Profile');

normally it should work now when you try the query

$u = $this->User->find('all',array('contain' => array('Profile'));

or

$u = $this->User->find('all',array('recursive' => 2));

but it also should work if you write only:

$u $this->User->find('all');

regards

Manish M Demblani
  • 910
  • 1
  • 10
  • 21
aspi
  • 1
0

If you bake this things is made your life easier.

Models: User:

    class User extends AppModel{
        var $name = 'User';
        public $hasMany = array(
            'Profile'=>array(
                'className' => 'Profile',
                'foreignKey' => 'user_id',)
        );    
}

Profile:

class Profile extends AppModel{
    var $name = 'Profile';
            public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
        )
    );

}

Controller: Users:

    class UsersController extends AppController{
        var $name = 'User';        

        public function index(){
            $users = $this->User->find('all'); 
            var_dump($users);
        }

} 

Controller Profile:

    class ProfilesController extends AppController{
            var $name = 'Profile';        

            public function index(){
                $user_id = 2;
                $users = $this->Profile->find('all', array('conditions'=>array('Profile.user_id'=>$user_id))); 
                var_dump($users);
            }

    } 
Fury
  • 4,643
  • 5
  • 50
  • 80
  • I tried this one but still no luck. I expect sql query to use some join. Presently query looks like:
    SELECT `Profile`.`id`, `Profile`.`role`, `Profile`.`user_id` FROM `test`.`profiles` AS `Profile` WHERE `Profile`.`user_id` = 1
    – Bilal Wani Feb 21 '14 at 06:07
  • Just wanted to add my sql queries, not sure what is wrong, CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(60)); CREATE TABLE profiles (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, role varchar(60), user_id INT, CONSTRAINT fk_usr_profile FOREIGN KEY(user_id) REFERENCES test.users(id)); – Bilal Wani Feb 21 '14 at 06:27
  • I see.. your database is not configured properly. or your saving statement in your controller is incorrect. If you have it live let me check it for you – Fury Feb 21 '14 at 07:40
  • I am using WAMP - MySql, I can insert data manually, so there is no issue. Even I know there is some issue with database, cannot show you live as I am working on localhost. Any inputs on database step would be really helpful. – Bilal Wani Feb 21 '14 at 09:19
  • Further if use $this->User->query("SELECT users.* , profiles.* from users INNER JOIN profiles ON users.id = profiles.user_id"); then it gives me expected result:Array ( [0] => Array ( [users] => Array ( [id] => 1 [name] => david ) [profiles] => Array ( [id] => 1 [role] => admin [user_id] => 1 ) ) ) – Bilal Wani Feb 21 '14 at 09:36
  • can you show me your save view and method in controller. sounds you trying to save id of NULL - I also added public to method try that – Fury Feb 21 '14 at 09:52
  • I have not added any 'save' method. As you would be aware in 'phpmyadmin' interface allows to insert data in tables directly. So in this case I have already inserted data in tables and there is ligitimate data in tables. That is reason why query() is working fine. – Bilal Wani Feb 21 '14 at 11:57
  • Any further comments, not able to fix it yet – Bilal Wani Feb 25 '14 at 07:51
  • if you have it live let me look at it. – Fury Feb 25 '14 at 16:44
  • how to make it live, any suggestions – Bilal Wani Feb 26 '14 at 18:44
  • I retried as per the tutorial, http://blog.the-nerd.be/2012/08/cakephp-basics-tutorial-part-4/. Things are working fine. User Profile Example – Bilal Wani Feb 26 '14 at 19:24