[Cake 3.x] Change database on the fly and multiple databases in one request

Hello,

I have one global database with acls and app main data - that is easy configured by function defaultConnectionName() and I have many databases with the same model data I tried to switch database by writing in user session which database is current working and it working but I am not satisfied with this solution:

         public static function defaultConnectionName() {
              $connectionName = 'default';
              $session = new Session();
              $community = $session->read('Community');
               if (isset($community['dbname']))
               $connectionName = $community['dbname'];
        	  return $connectionName;
        	  
         } 

and in AppController:

$session = $this->request->session();
$community = $session->read('Community'); 
if($community != null && isset($community['dbname']))            
    \Cake\Datasource\ConnectionManager::alias($community['dbname'], 'default');

It’s working in one request I could have one database but maybe someone knows betters solutions ?

Also I tried to implement a supercontroller which access in one request to many databases:

function index() {

 $communities = $this->Communities->find('all')->toArray() // is model form main database which have name of datasource;
        $session = $this->request->session();
        $tmp = [];
        foreach($communities as $id => $community){
                        //
             \Cake\Datasource\ConnectionManager::alias($community->dbname,'default');
            // or tried to change database by sessions data
            //$session->write('Community',$community->toArray());
          
            
            $people = $this->People->find('all'); // model which is present in many databases 
            $people->limit(1);
            $people->where(['People.surname LIKE' =>  'Kowlask%']);
            $tmp = $people->toArray();
        
           
        }
       debug($tmp);
       exit; 
	   
 }

And I the result for this index function is always from the first database connection name foreach but I don’t understand why it that happend…