Can not use secondary Sqlserver database

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! :slight_smile:

Exactly which version of CakePHP are you using for this? In Cake v4, the function declaration is

public static function defaultConnectionName(): string

not

public static function defaultConnectionName()

I’m using CakePHP 4, should have mentioned that.

How would i use the function? Should I replace the word “string” with ‘db2’?

No, you should literally add : string to the end of your function declaration. The part from { to } is fine.

Thank you very much, that did the trick! Now everything is working.

However, you did me unsure at first, but I did follow the cookbook for version 4. Im urging the devs/cockbook authors to correct the following page which has the old info:

https://book.cakephp.org/4/en/orm/table-objects.html#configuring-connections

The documentation is all in the git repository somewhere, you can open issues with it through that. They’re unlikely to see your comment here.

1 Like

Done and done! I have submitted a change request on Github. Thanks again!

1 Like