My Application need to retrieve data from both MySQL and MSSQL servers.
Most of tables are in MySQL so I make it default datasourse.
I also configure a custom datasourse as below
I am using CakePHP 4.2.8, PHP v7.4.23.
The hostname follow by the Instance name. MSSQL can have multiple Instances running on the same host, and each instance can be turned off/of separately.
I have tested the hostname, user, password, and DB name with another PHP simple script, connection can be established using PDO_SqlSrv driver.
$serverName = "hse-db\\ihrp"; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"db_name", "UID"=>"user", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
The thing does not work is I got error message in IDE, it says expecting type Cake\Database\Connection but found String at the line $this->setConnection(‘fam’);
Right. Well, the error you’re getting is because the function is expecting a connection object, not a string. Try $this->setConnection(ConnectionManager::get('fam'));