Cakephp $useTable does not work

Cristhian Boujon picture Cristhian Boujon · Oct 24, 2012 · Viewed 11.7k times · Source

I'm developing a web app on CakePHP 2.2.3 with an already database(MySQL) existing. This database is in spanish language so my app is in spanish and I get the error "Error: Table ordens for model orden was not found in datasource default."

My Model:

Orden.php

class Orden extends AppModel{
    public $name = 'Orden';
    public $useTable = 'ordenes';
    public $primaryKey = 'idorden';

}

Table:

CREATE TABLE IF NOT EXISTS `ordenes` (
  `idOrden` bigint(20) NOT NULL AUTO_INCREMENT,
  -- more columns --
  PRIMARY KEY (`idOrden`)
)

APP/config/database.php

class DATABASE_CONFIG {

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'USER',
    'password' => 'PASS',
    'database' => 'DATABASE',
    'prefix' => '',
    //'encoding' => 'utf8',
);

}

According to docs I can declare $useTable with the corresponding table but this does not work. Can anyone help me, please?

EDIT: Same problem with $primaryKey but variable $name has the right value.

Answer

Cristhian Boujon picture Cristhian Boujon · Oct 25, 2012

I found a half and ugly solution. Check the following code:

class OrdenesController extends AppController{

    public $name = 'Orden'; 
    public $uses = array('orden'); 
    public $helpers = array('Html', 'Form');

    public function index(){
        $this->orden->useTable = 'ordenes';
        $this->set('ord', $this->orden->find('all'));      
    }
}

If I set the variable $useTable in OrdenesController, I can get the array of Ordenes. Can anyone tell me why?