Welcome Guest, Not a member yet? Register   Sign In
$db is confusing in the docs.

When I enter the docs and see something like this:

$fields = $db->getFieldNames('table_name');

It confuses the hell out of me to figure out WHERE $db 'magically' comes from ... I see it NOWHERE in MY ResourceControllers ... and if I try to define it all hell breaks loose ...

Now, please add some understandable CONTEXT when writing docs.

Can someone please shed some light on HOW to understand the docs when trying to use it as a reference?

(This post was last modified: 12-06-2020, 09:40 AM by captain-sensible.)

here's the thing blaasvaer,

Quote:Now, please add some understandable CONTEXT

don't take this the wrong way as a have a sense of humor that is too dry for some but ..i wonder if you can see the irony of your statement.

Your post is not in CI4 nor CI3 so i have no context as to which codeigniter you are using .

But lets look at CI4 since thats the latest. In app/config/Database.php you define which database you are using. Then using :
$db = \Config\Database::connect();

the above gets a connection for you. With no parameters passed to connect() it assumes config from default.
$db2 = \Config\Database::connect('custom');

above uses custom config also in app/config/Database.php

then i can use :

the reason i can do that is that my table and fields is already modelled.

If you can elaborate a little on your problem then i'm sure somebody will help
CMS CI4     I use Arch Linux by the way 

Arch Book

Harh, though that clicking a certain thread in the forums would have automatically put me in the CI version 4 forums ... hey, what do I know about being creating smart links in the forums ; )

But, thanks for pointing that out. What keep kicking me is that when I enter the docs, I do that from a certain 'place of mind' if you will ... in my case a ResourceController. So when I se $db-> ... I automatically assumes it relates to a ResourceController ... my bad, of course. Since I have to use it like this:

$this->db ... in my case.

(This post was last modified: 12-06-2020, 11:33 AM by captain-sensible.)

in a model i use this:
public function getAll()

    $this->db = \Config\Database::connect();
    return $this->findAll();
    //return $this->orderBy('Id', 'desc')->Res();

so from controller it would be $modelHandler->getAll();

not sure what you mean by "ResourceController" i only have BaseController from which my own controllers are derived ?

i must add i have returned to CodeIgniter after a few years of using other frameworks, so i might need a heads up if some terminology is legacy related

notice you also have a post about debugging , do you want to consolidate all issues into one post or ..
CMS CI4     I use Arch Linux by the way 

Arch Book

Any time you open up a model in CodeIgniter it sets the $db see below Model constructor.

PHP Code:
     * Model constructor.
     * @param ConnectionInterface|null $db
     * @param ValidationInterface|null $validation
public function __construct(ConnectionInterface &$db nullValidationInterface $validation null)
$db $db ?? Database::connect($this->DBGroup);

$this->db                 = &$db;
$this->tempReturnType     $this->returnType;
$this->tempUseSoftDeletes $this->useSoftDeletes;
$this->tempAllowCallbacks $this->allowCallbacks;
$this->validation         $validation ?? Services::validation(nullfalse);

You can also connect like so:

PHP Code:
// The long way.
$db = \Config\Database::connect($groupName);

// helper way
$db db_connect($groupName); 

Nothing really confusing about it.

Even if you use a Model the $this->db is there along with QueryBuilder.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Theme © iAndrew 2016 - Forum software by © MyBB