CodeIgniter Forums
CI4 where should I connect to DB? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: CI4 where should I connect to DB? (/thread-76383.html)



CI4 where should I connect to DB? - imabot - 05-07-2020

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 ?



RE: CI4 where should I connect to DB? - php_rocs - 05-07-2020

@imabot,

This documentation may assist you... https://codeigniter.com/user_guide/models/model.html#using-codeigniter-s-model


RE: CI4 where should I connect to DB? - InsiteFX - 05-08-2020

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.


RE: CI4 where should I connect to DB? - jreklund - 05-08-2020

$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/models/model.html#manual-model-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();



RE: CI4 where should I connect to DB? - imabot - 05-09-2020

(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/models/model.html#manual-model-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?


RE: CI4 where should I connect to DB? - jreklund - 05-10-2020

(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.


RE: CI4 where should I connect to DB? - imabot - 05-10-2020

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?


RE: CI4 where should I connect to DB? - InsiteFX - 05-10-2020

PHP Code:
$db      = \Config\Database::connect();
$builder $db->table('tableName'); 



RE: CI4 where should I connect to DB? - jreklund - 05-10-2020

@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/models/model.html#configuring-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();



RE: CI4 where should I connect to DB? - imabot - 05-11-2020

(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/models/model.html#configuring-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.