Welcome Guest, Not a member yet? Register   Sign In
How do I overwrite the protected table in Model?
#1

Hi,
My Model has this:
PHP Code:
protected $table 'users'

My library:
PHP Code:
public function __construct()
    {
        $this->AuthModel = new AuthModel();
        $this->Session session();
        $this->request Services::request();      
    

And I have a method with:
PHP Code:
$user $this->AuthModel->where('email'$email)->first(); 
However, for this method, I need to access a different table. So I tried and it didn't work:
PHP Code:
$user $this->AuthModel->table('staff)->where('email', $email)->first(); 
Is there a way I can overwrite the
Code:
protected $table = 'users';

with:
Code:
protected $table = 'staff';
only for this method or pass the table I need?
Cheers
Reply
#2

You are going to completely wrong way.
The $table in the CodeIgniter\Model is the main table for the model.
You should not change it at runtime and do not need to do it.

Why don't you have UserModel and StaffModel?
Reply
#3

I thought about that, but I was not sure if it was ok for a library to have more than one model. I don't remember seeing other libraries with multiple models. So, I thought it could be a bad practice.
Reply
#4

Here is a tutorial that uses a library to access two models.

How to build a basic web application with CodeIgniter 4 (part 4)

includebeer tutorials - The MyRecipes library.

See the The MyRecipes library. I would read the complete tutorial.
What did you Try? What did you Get? What did you Expect?

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

I got it. Thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB