i setup my first Codeigniter4 project. I failed on adding the database-connection to the basecontroller to make the db-connection avivable in all controllers/models/helpers etc. to avoid setup it again and again in every file.
So like the docu i think i have to do it with the basecontroller like this
...
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
//Create global instance for DB-Connections
//$globalDB = db_connect('globalDB');
//$this->globalDB = ConfigDatabase::connect('globalDB'); //db_connect('globalDB');
...
}
if i try to work with it inside a modell, it failed
return $globalDB->query('SELECT * FROM mytable LIMIT 1;')->getResultArray();
//return $this->globalDB->query('SELECT * FROM mytable LIMIT 1;')->getResultArray();
i get error messages like this “Undefined variable $globalDB”
So what am i doing wrong, maybe i missundestood something?