Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 V 4.5.1 $this->db problem
#1

I updated to version 4.5.1 and now $this->db is protected, how can I query tables other than the one set in the model in $table?
Until previous versions it was sufficient to do:
PHP Code:
$this->db->table('mytable')...  
and it was possible, now what should I do to maintain the same functionality?
Am I forced to change the value of $table before each query in different tables?

What is the correct direction?
Reply
#2

See https://codeigniter.com/user_guide/model...ther-table
Reply
#3

The documentation is not clear to me, can you give me a practical example
I can't do as in CI3 and continue using $this->db->table(), are there any problems with this?
PHP Code:
public function __construct()
{
    parent::__construct();

Now if I do 
PHP Code:
$this->db->table('mytable')  
it works, is this a correct approach?
Reply
#4

Are you talking about Controller code? or model code? or something else?

In PHP, $this->db means the $db property in the object.
In CI4, there is no $this->db in controllers by default.
Reply
#5

I'm talking about model code
Reply
#6

In the Model (BaseModel) class, there is the protected $db property.
So you can use it in your model.

Quote:Members declared protected can be accessed only within the class itself and by inheriting and parent classes.
https://www.php.net/manual/en/language.o...visibility
Reply
#7

I think I don't understand.

If in my model I do: 
PHP Code:
$this->db->table('mytable')...  
I get the error: Call to a member function table() on null
Shouldn't $this->db be available in the model?
it is correct to use:
PHP Code:
parent::__construct();  
in the model construct?
You're too telegraphic and I don't understand, I would like to understand how to get back to having the same thing as before the update but in the correct way.

There are models where in addition to using the db connection to the table assigned to the model, I have to execute queries on other tables or there are models where I execute queries on different tables and I don't have a dedicated one for the model
Reply
#8

(This post was last modified: 05-17-2024, 06:36 AM by kenjis.)

(05-17-2024, 06:27 AM)serialkiller Wrote: it is correct to use:
PHP Code:
parent::__construct();  
in the model construct?

Of course not. It is incorrect. It makes Model not working.

CodeIgniter is build on PHP. It is difficult to use it without knowing how PHP works.
Reply
#9

(05-17-2024, 06:27 AM)serialkiller Wrote: You're too telegraphic and I don't understand, I would like to understand how to get back to having the same thing as before the update but in the correct way.

What version did you upgrade to 4.5 from?
My understanding is that there are no particular breaking changes,
so I'm not sure why your code would stop working with the upgrade.
Reply
#10

I updated from 4.46 to 4.5.1, I've made all the changes to every file in every upgrade from 4.4.6 to 4.5.1 and everything works except this one

The model works for both methods only with the construct

So how should I do to be able to use $this->db->table('mytable') ?


PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
My_model extends Model
{
    protected $table      'my_table';

    protected $primaryKey 'id';

    protected $allowedFields = [
        'field_1',
        'field_2',
        'field_3',
        'field_4',
    ];


    public function test_1()
    {
        $result $this->select('field_1, field_2')
                      ->where('id'1)
                      ->get()
                      ->getRow();
    }


    public function test_2()
    {
        $user $this->db->table('user')
                        ->select('name, surname')
                        ->where('id'1)
                        ->get()
                        ->getRow();
    }




or


PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
My_model extends Model
{
    public function test_1()
    {
        $result $this->db->table('product')
                          ->select('field_1, field_2')
                          ->where('id'1)
                          ->get()
                          ->getRow();
    }


    public function test_2()
    {
        $user $this->db->table('user')
                        ->select('name, surname')
                        ->where('id'1)
                        ->get()
                        ->getRow();
    }




For example, if I have a model like this, how do I make the test_2 method work?
The test_2 method gives me the error: Call to a member function table() on null
if instead I put the construct both works

PHP Code:
public function __construct()
    {
        parent::__construct();
    
Reply




Theme © iAndrew 2016 - Forum software by © MyBB