Problems relating CakePhp Templates

0

This is the Model User

class User extends AppModel
{
public $name = 'User';
public $useTable = 'users';
public $displayField = 'name';
public $belongsTo = array(
        'Role' => array(
        'className' => 'Role',
        'foreignKey' => 'role_id'
    )
);
}

and this is the Model Role

class Role extends AppModel
{
public $name = 'Role';
public $useTable = 'roles';
public $displayField = 'name';
public $hasMany = array(
        'User' => array(
        'className' => 'User',
        'foreignKey' => 'role_id',
        'dependent' => false
    )
);

}

however when trying to make the following request

$roles = $this->User->Role->find('list');

These are the tables referenced by Models

CREATE TABLE IF NOT EXISTS 'roles' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'name' varchar(100) NOT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS 'users' (
  'id' int(10) NOT NULL AUTO_INCREMENT,
  'email' varchar(50) DEFAULT NULL,
  'password' varchar(50) DEFAULT NULL,
  'name' varchar(50) DEFAULT NULL,
  'activation_code' varchar(50) DEFAULT NULL,
  'status' int(10) DEFAULT NULL,
  'created' datetime DEFAULT NULL,
  'modified' datetime DEFAULT NULL,
'role_id' int(10) DEFAULT NULL,
PRIMARY KEY ('id'),
 KEY 'FK_users_roles' ('role_id'),
CONSTRAINT 'FK_users_roles' FOREIGN KEY ('role_id') REFERENCES 'roles' ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

It is reported that Role is a null object, does anyone know what is wrong?

    
asked by anonymous 04.04.2014 / 16:09

1 answer

1

I'll give you an example of two Models that work perfectly for this goal

Model User.php

<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Group $Group
 */
class User extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'group_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

Model Group.php

<?php
App::uses('AppModel', 'Model');
/**
 * Group Model
 *
 * @property User $User
 */
class Group extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'group_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}
    
04.04.2014 / 16:54