Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Williams Duarte

Especificar o tipo de banco de dados CakePHP 3.0

Recommended Posts

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';
}

Já na versão do CakePHP 3.0 como atribuir estas configurações na Model?

<?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. :(

Compartilhar este post


Link para o post
Compartilhar em outros sites

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'],
	],
],

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.