Problems Relating Models in Cakephp

Asked

Viewed 142 times

0

This and 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 and 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
    )
);

}

but 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;

is informed that Role is a null object, someone knows what is wrong?

  • cake considers the object '$this -> User -> Role' to be null, so I am including Role in the user control uses and using it as an incremented model, but this is not the right way :S

  • user, in this case does not work, because User belongs to Role and not the other way around. You need this find for what? If you were calling in this way, inside Rolescontroller it would work, but inside Userscontrollers it will not work, because each user only belongs to a single Role, would not have what to list in this case.

1 answer

1

Let me give you an example of two Models that work perfectly for this purpose

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' => ''
        )
    );
}
  • still not working, could be some problem in the database? , Role should have your Controller?

  • Yes, each of the models must have a controller

  • Could you put the structure of your database in the question?

  • As was the case with the models now and the database tables exported as SQL script, I created a controller for Role, but without methods, because I am not interested in changing, your data must be static 1-> ADM, 2-> USER 3 -> PUBLIC

  • 1

    You don’t necessarily need a controller, eventually I get 2 or 3 models for a single controller.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.