How to use database in CakePHP without complying with the naming conventions?

9

CakePHP has conventions for naming tables, classes, etc., such as having the username and password columns for tables that hold users. In my current situation, I have tables that do not meet these standards.

How could I continue to use the features and features of the framework without changing the names of all tables and columns?

    
asked by anonymous 13.01.2014 / 15:17

2 answers

8

You can force the table name into the model definition:

class MeuModel extends AppModel {
    public $useTable = 'minha_tabela';
}

Columns can have the name you want, and it is recommended that the table has a PK with autoincrement called id . If your PK has another name, you can force one, but it needs to be a simple PK (ie, not multiple columns):

class MeuModel extends AppModel {
    public $useTable = 'minha_tabela';
    public $primaryKey = 'minha_tabela_id';
}

For the username and password columns, names can be set when you include the Auth component on the Controller. For example, to use a field named email instead of username :

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

More details on the Authentication Handbook section .

    
13.01.2014 / 15:25
1

Complementing @bfavaretto's response, do not forget to call the model in the controller.

QualquerController extends appController {
    // $uses atribui o model 'MEU'.
    public $uses = 'Meu';
}

This solves the problem of the inflector compelling to call its 'My' controller, to use a 'My' model.

    
26.02.2014 / 17:12