CodeIgniter Forums
Making $db available globally - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Making $db available globally (/showthread.php?tid=80309)



Making $db available globally - CodingInCodeigniter - 10-15-2021

Taken from https://codeigniter.com/user_guide/database/connecting.html



Quote:You can connect to your database by adding this line of code in any function where it is needed, or in your class constructor to make the database available globally in that class.


When i do exactly that:

Code:
<?php

namespace App\Models\Admin;

use CodeIgniter\Model;

class AccessModel extends Model
{

  function __construct()
  {
    $db = \Config\Database::connect();
  }
 
  // This function does the log in and stores the admin info from to DB to a session array
  public function login($username,$password)
  {
    $query  = $db->query('SELECT password, email FROM admin');
    $results = $query->getResult();
    var_dump($results);die();
  }
i get the following error:

ErrorException

Undefined variable $db


I also tried adding it into the BaseController which im trying to use as Ci3's version of an autoloader but i get the same error
Code:
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);     

        // Preload any models, libraries, etc, here.
        helper('form');
        $db = \Config\Database::connect();


Kindly point me in the right direction.


RE: Making $db available globally - includebeer - 10-16-2021

You are missing some OOP notions. In your example, $db is a local variable, not a class property. You need to assign it to $this->db.

But, I wouldn't advise you to do that. Database queries should be done in models, not in controllers. CI is very flexible and won't prevent you from doing database requests everywhere, but your application will be hard to maintain and debug if you have SQL all over the place instead of following the MVC pattern.


RE: Making $db available globally - sr13579 - 10-16-2021

You should simply use ,
db_connect()->table('table_name')->...->...;
This one is already enough global.


RE: Making $db available globally - iRedds - 10-16-2021

The controller is not the place for database queries


RE: Making $db available globally - donpwinston - 10-17-2021

(10-16-2021, 09:11 PM)iRedds Wrote: The controller is not the place for database queries

You can still create a database object in a controller and invoke a method that queries or updates the database. You don't need to use the CI model class.

He/she could do this in the BaseController and then it would be sort of "global".


RE: Making $db available globally - includebeer - 10-17-2021

(10-17-2021, 12:55 AM)donpwinston Wrote:
(10-16-2021, 09:11 PM)iRedds Wrote: The controller is not the place for database queries

You can still create a database object in a controller and invoke a method that queries or updates the database. You don't need to use the CI model class.

He/she could do this in the BaseController and then it would be sort of "global".

It is technically possible but not a best practice. If it's not in a model class it should at least be in a library.