Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Nas versões do CakePHP 2.x.x, é só especificar nos atributos as configurações que desejo usar, o que da a flexilidade de se trabalhar com diversos bancos.
<?php
//2.x.x
App::uses('Model', 'Model');
class MyModel extends AppModel {
public $name = 'MyModel';
public $useTable = 'name_table';
public $primaryKey = 'id_table';
public $useDbConfig = 'name_database';
}<?php
//3.0
namespace App\Model\Table;
use Cake\ORM\Table;
class MyTable extends Table
{
}
Atenciosamente.
Ps.: Já testei as do 2.x.x, mas sem sucesso. :(
Valeu Marcos!
Fica a Dica para que quiser usar além das configurações de acesso default
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class myTable extends Table
{
public static function defaultConnectionName() {
return 'other_database';
}
public function initialize(array $config) {
$this->table('prefix_table');
$this->primaryKey('my_id');
}
}
config/app.php
/**
* Connection information used by the ORM to connect
* to your application's datastores.
* Drivers include Mysql Postgres Sqlite Sqlserver
* See vendor\cakephp\cakephp\src\Database\Driver for complete list
*/
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'nonstandard_port_number',
'username' => 'root',
'password' => '123456',
'database' => 'vialojac_newsletter',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
/**
* Set identifier quoting to true if you are using reserved words or
* special characters in your table or column names. Enabling this
* setting will result in queries built using the Query Builder having
* identifiers quoted when creating SQL. It should be noted that this
* decreases performance because each query needs to be traversed and
* manipulated before being executed.
*/
'quoteIdentifiers' => false,
/**
* During development, if using MySQL < 5.6, uncommenting the
* following line could boost the speed at which schema metadata is
* fetched from the database. It can also be set directly with the
* mysql configuration directive 'innodb_stats_on_metadata = 0'
* which is the recommended value in production environments
*/
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
/**
* The test connection is used during the test suite.
*/
'other_database' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
//'port' => 'nonstandard_port_number',
'username' => 'root',
'password' => '123456',
'database' => 'test',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
],Perfeito. Estou precisando estudar o CakePHP 3 mas estou em uma correria daquelas.
Dá uma olhada ae http://book.cakephp.org/3.0/en/orm/table-objects.html#basic-usage