Peter Krauss
Peter Krauss

Reputation: 13920

How to set table schema at table name with Cake?

PostgreSQL, Oracle and many other DBMS's use SCHEMA, so, the table name is

schema_name.table_name

But CakePHP manuals not say anithing about this. What about Model, View and Controller names in the CakePHP defaults? I can use a solution like prefix, that is, where the same schema name will be used at all database operations.


PS1: please not to be confused with method Modelschema, and questions about accessing this method.

PS2: the Bill's 2006 solution is not the better one, because is not updated (I am using CakePHP2) and is not a "official cakePHP solution".

PS3: database.php have some schema attribute? What the link to CakePHP documentation?

Upvotes: 0

Views: 1768

Answers (2)

Lady Mea
Lady Mea

Reputation: 11

In CakePHP you must define more database config.

In CakePHP 2:

  • set the 'schema' param to your config
  • create new configs for all of your schema
  • use the right schema in your models

For example, database conf:

public $default = array(
        'datasource' => 'Database/Postgres',
        'persistent' => false,
        'host'       => 'localhost',
        'login'      => 'my_db_user', 
        'password'   => 'my_db_passw', 
        'database'   => 'my_project_db', 
        'prefix'     => '',
        'encoding'   => 'utf8',
        'schema'     => 'postgres'
    );
public $other_schema = array(
        'datasource' => 'Database/Postgres',
        'persistent' => false,
        'host'       => 'localhost',
        'login'      => 'my_db_user', 
        'password'   => 'my_db_passw', 
        'database'   => 'my_project_db', 
        'prefix'     => '',
        'encoding'   => 'utf8',
        'schema'     => 'other_schema'
    );

If you want to use it in a model:

class AppModel extends AppModel {
    public $useDbConfig = 'other_schema';
}

In CakePHP3 is the same way, just there the database is config/app.php and you must use

use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');

Upvotes: 1

Peter Krauss
Peter Krauss

Reputation: 13920

Good news for me, there are CakePHP 2.0 documentation about SQL-Schema... No other documentation or examples, but a starting point...

Upvotes: 1

Related Questions