Welcome Guest, Not a member yet? Register   Sign In
CI4 where should I connect to DB?
#1

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 ?
Reply
#2

(This post was last modified: 05-08-2020, 08:19 AM by jreklund.)

@imabot,

This documentation may assist you... https://codeigniter.com/user_guide/model...er-s-model
Reply
#3

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

$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();
Reply
#5

(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?
Reply
#6

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

(This post was last modified: 05-10-2020, 06:03 AM by imabot.)

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?
Reply
#8

(This post was last modified: 05-10-2020, 06:59 AM by InsiteFX.)

PHP Code:
$db      = \Config\Database::connect();
$builder $db->table('tableName'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

@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();
Reply
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB