Cakephp is ignoring my model

Asked

Viewed 69 times

0

I have an "Admin" plugin and in it I have the following: Userscontroller.php (Userscontroller), User.php (is the Model User). In this case, I am saving user data, the controller correctly saves the data, Stretanto the validation functions, password encryption and any other that I put in the controller, even for testing, do not load. Goes down:

Userscontroller.php (Userscontroller) ---

class UsersController extends AdminAppController {

    public $name = 'Users';
    var $helpers = array('Form', 'Html');

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

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();

            if(isset($this->data)){
            $this->User->set($this->data); 
                if ($this->User->validates()){
                    if ($this->User->save($this->request->data)) {
                          $this -> Session -> setFlash('Usuario cadastrado!');
                          $this -> Redirect(array('action' => 'index'));
                          exit();
                    } else {
                        $this -> Session -> setFlash('Nao foi possível, cadastrar usuario. Por favir aguarde!');
                        $this -> Redirect(array('action' => 'add'));
                        exit();                
                    }
                }
                else{
                     $this -> Session -> setFlash($this->User->invalidFields());
                     $this -> Redirect(array('action' => 'add'));
                     exit();                
                }
            }
        }
    }
}

User.php (is the User Model) ---

class User extends AppModel {
    public $name = 'User';

   public $validate = array(
        'username' => array(
            'rule' => 'notEmpty',
            'message' => 'Nome obrigatorio'
        ),
        'password' => array(
            'rule' => 'notEmpty',
            'message' => 'Senha Obrigatoria'
        ),
        'email' => array(
            'rule' => 'notEmpty',
            'message' => 'E-mail obrigatorio'
        ),
        'group_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Grupo obrigatorio'
        )
    );

    public function beforeSave($options = array()) {
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }
}

Table in the dataset is users

1 answer

0


Your model is inside the plugin?

If you have try to use in the controller:

public $uses = array(
    'Admin.User',
);

If not in the plugin use:

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

Make these calls before the Helper calls in your controller.

  • First of all, thank you, you solved my problem. What I didn’t understand was why it wasn’t working without adding this to the controller, because according to what I read in the Cakephp documentation, if you follow the naming conventions, that part should be "automatic", and leave those things "automatic" is the main reason to follow the conventions. But fortunately it’s working now, thanks again.

  • So Charles, probably what happened, is that your Model is at the root of the project, and your Controller is in the Admin plugin. When this is the case, the plugin’s controller does not inherit the root model by name. When the two are in the same plugin, or at the root of the project, by cakephp convention, it is not necessary to use $uses when the two have the same name, model = singular, controller = plural.

Browser other questions tagged

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