Jeden z kontrolerów używa bazy danych na zdalnym serwerze
app.php
'remote_db' => [ 'className' => Connection::class, 'driver' => Mysql::class, 'persistent' => false, 'timezone' => 'UTC', 'flags' => [], 'cacheMetadata' => true, 'log' => false, 'quoteIdentifiers' => false, ],
app_local.php
'remote_db' => [ 'driver' => 'mysql', 'username' => '...', 'password' => '...', 'database' => '...', 'host' => 'remote.example.com', // zapis odczyt do zdalnej bazy /* 'read' => [ 'host' => 'remote.example.com', // czytamy zdalną bazę ], 'write' => [ 'host' => 'localhost', // zapisujemy do lokalnej ] */ ];
Table
namespace App\Model\Table;
use Cake\ORM\Table;
class ArticlesTable extends Table
{
public static function defaultConnectionName(): string {
return 'remote_db';
}
public function initialize(array $config): void
{
$this->setEntityClass('App\Model\Entity\Article');
$this->setTable('my_table');
$this->setPrimaryKey('my_id');
}
}
Controller
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('remote_db');
$query = $connection->selectQuery();
$query->select('*')
->from('articles')
->where(['published' => true]);
$query->useReadRole(); $query->useWriteRole();