CodeIgniter Forums

Full Version: CI4 where should I connect to DB?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm writing models that use query builder with joints and many other funny stuf. Since CI4, we need to manually connect to the DB :

Code:
$db = \Config\Database::connect()

Where should I place this connection line of code :
  • when I have to use $db in several models ?
  • when I use $db only in one models ?
@imabot,

This documentation may assist you... https://codeigniter.com/user_guide/model...er-s-model
PHP Code:
Class NewModel extends Model
{
    protected $db;

    public function __construct()
    {
        $this->db db_connect();

        // or
        $this->db = \Config\Database::connect();
    }



You can then access the database using $this->db any where in your model.
$this->db are already initialized in Model, no need to connect again. Only if you don't want to extend the "helper" model created for you.
https://codeigniter.com/user_guide/model...l-creation

You can also use all function from the query builder directly.

You don't need to do:
Code:
$this->db->countAll();

You can do:
Code:
$this->countAll();
(05-08-2020, 08:25 AM)jreklund Wrote: [ -> ]$this->db are already initialized in Model, no need to connect again. Only if you don't want to extend the "helper" model created for you.
https://codeigniter.com/user_guide/model...l-creation

You can also use all function from the query builder directly.

You don't need to do:
Code:
$this->db->countAll();

You can do:
Code:
$this->countAll();

Perfect answer, thank you. What if I have several database, are they all initialized automatically?
(05-09-2020, 10:49 PM)imabot Wrote: [ -> ]Perfect answer, thank you. What if I have several database, are they all initialized automatically?

No, that you are required to handle on your own.
Oops, it does not work :


Quote:You must set the database table to be used with your query.

Here is my code:
Code:
$builder = $this->table('tablename');
        $builder->orderBy('id', 'DESC');
        $builder->orderBy('timestamp', 'DESC');
        $builder->where('field', $field);
        $sqlQuery  = $builder->get();       
return $sqlQuery->getResult();


It only works with manual initialization. What am i doing wrong?
PHP Code:
$db      = \Config\Database::connect();
$builder $db->table('tableName'); 
@imabot: If that's in the model? For the primary database? If so, you need to configure your model first:
https://codeigniter.com/user_guide/model...your-model

You don't need to use $this->table.

You just do:
Code:
$this->orderBy('id', 'DESC');
$this->orderBy('timestamp', 'DESC');
$this->where('field', $field);
$sqlQuery  = $this->get();
return $sqlQuery->getResult();
(05-10-2020, 08:47 AM)jreklund Wrote: [ -> ]@imabot: If that's in the model? For the primary database? If so, you need to configure your model first:
https://codeigniter.com/user_guide/model...your-model

You don't need to use $this->table.

You just do:
Code:
$this->orderBy('id', 'DESC');
$this->orderBy('timestamp', 'DESC');
$this->where('field', $field);
$sqlQuery  = $this->get();
return $sqlQuery->getResult();

OK, got it. Thank you.