Hello,
Im developing an web app for my work and it will consist of a dedicated SQL Server database with all the info but the customer info should be retrieved/managed from another SQL Server database.
I can’t seem to get the CustomersTable.php to work correctly. In addition to being another database, the tables in the customer database does not follow naming conventions.
app.php:
‘Datasources’ => [
‘default’ => [
‘className’ => ‘Cake\Database\Connection’,
‘driver’ => ‘Cake\Database\Driver\Sqlserver’,
‘persistent’ => false,
‘host’ => ‘hostname’,
‘port’ => 1433,
‘username’ => ‘sa’,
‘password’ => ‘password’,
‘database’ => ‘db1’,
‘encoding’ => PDO::SQLSRV_ENCODING_UTF8,
‘timezone’ => ‘UTC’,
‘cacheMetadata’ => true,
‘quoteIdentifiers’ => false,
],
‘db2’ => [
‘className’ => ‘Cake\Database\Connection’,
‘driver’ => ‘Cake\Database\Driver\Sqlserver’,
‘persistent’ => false,
‘host’ => ‘hostname’,
‘port’ => 1433,
‘username’ => ‘sa’,
‘password’ => ‘password’,
‘database’ => ‘Db2’,
‘encoding’ => PDO::SQLSRV_ENCODING_UTF8,
‘timezone’ => ‘UTC’,
‘cacheMetadata’ => true,
‘quoteIdentifiers’ => false,
],
],
CustomersTable.php:
<?php
// src/Model/Table/CustomersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
class CustomersTable extends Table {
public static function defaultConnectionName() {
return 'db2';
}
public function initialize(array $config): void {
$this->setTable('tblCustomer');
$this->addBehavior('Timestamp');
}
}
Customer.php:
<?php
// src/Model/Entity/Customer.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Customer extends Entity
{
protected $_accessible = [
'*' => true,
'Id' => false,
];
}
CustomersController.php:
<?php
// src/Controller/CustomersController.php
namespace App\Controller;
use Cake\Datasource\ConnectionManager; // This line is required
class CustomersController extends AppController
{
public function index()
{
$this->loadComponent('Paginator');
$customers = $this->Paginator->paginate($this->Customers->find());
$this->set(compact('customers'));
}
}
View (index.php):
<!-- File: templates/Customers/index.php -->
<h1>Customers</h1>
<table>
<tr>
<th>Cust. No</th>
<th>Name</th>
<th>City</th>
<th>Modified</th>
</tr>
<!-- Here is where we iterate through our $customers query object, printing out article info -->
<?php foreach ($customers as $customer): ?>
<tr>
<td>
<?= $customer->nr ?>
</td>
<td>
<?= $customer->name ?>
</td>
<td>
<?= $customer->city ?>
</td>
<td>
<?= $customer->modified->format(DATE_RFC850) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Error message I get is:
Fatal error : Declaration of App\Model\Table\CustomersTable::defaultConnectionName() must be compatible with Cake\ORM\Table::defaultConnectionName(): string in D:\home\site\wwwroot\src\Model\Table\CustomersTable.php on line 6
I cant find any information about this error message anywhere.
Except for the CustomersTable.php every script looks like the other pages I have (Networks and Locations) which are all working fine. They are however using the default connection and the tables is following naming conventions.
Thanks for all the help!