0

We are using a connection manager to do the process but we are unable to achieve the thing that we needed. we tried different ways to unable to connect to dynamic multiple databases. below is the code which we are using.

php 5.6 above with cakephp 3.7 versions.

use Cake\Controller\Component;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
use Cake\Database\Connection; 
use Cake\Database\Driver\Mysql;

public function setDatabase($company_id, $datasource = 'default') {
       $dbConf = ConnectionManager::getConfig($datasource); //Here connect to dynamic database.
       $dbConf['database'] = "db_".$company_id;//changing database name to which we need to connect with same configurations
       $dbConf['name'] = "company";
       $dbConf['encoding'] = 'utf8';
       ConnectionManager::setConfig('company', $dbConf );
       // ConnectionManager::config('company', $dbConf );
       $JobSeekers = TableRegistry::get('JobSeekers');
       $JobSeekers_details = $JobSeekers->find('all'); // firing query jobseekers table but still it's excueting in default db instead of company db.which trows error
       $JobSeekers_data = $JobSeekers_details->first();
      pr($JobSeekers_data);
}

What we need is jobseeker query must be execute in company level database but we are getting following error which says connection still in default db.

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'default_db.job_seekers' doesn't exist
PHP CODER
  • 1,553
  • 4
  • 17
  • 34

0 Answers0