Welcome Guest, Not a member yet? Register   Sign In
How to access more than one table in the same model
#1

Hello

given that reading the guide the given Model should manage one table, I'm wondering if my question is acceptable

or if my approach is wrong

So first of all thank you for hinting about the right approach

Aiming to have Models for the DB queries operations,

My question is related to the fact I have a complex query with some joins which require the access to two more tables in the same DB.

E.G.
db name : tickets

Model is TicketModel which is set to work with 'ticket' table

but in the TicketModel I need to write a method more or less like


PHP Code:
    public function getTicketList()
    {
        return $this->select('ticket.ticket_id, ticket.subject, dept.name AS department, ticket.name AS user, ticket.custom_text, ticket.lastpost, post.message AS last_message')
            ->join('dept''dept.dept_id = ticket.dept_id')
            ->join('post''post.id = ticket.lastpost')
            ->groupBy('ticket.ticket_id')
            ->orderBy('ticket.ticket_id''ASC');
    


well, how do I "enable" the 'dept' and 'post' tables?

Is this possible in the same Model? If yes, how to?

Or should I anyway have three models or Entity class and then manage the access to the three tables at Controller level?

Thank you for pointing out the right approach
Reply
#2

(05-19-2023, 11:55 AM)Corsari Wrote: well, how do I "enable" the 'dept' and 'post' tables?

The code seems fine.
What do you mean by enable? What do you want to do?

(05-19-2023, 11:55 AM)Corsari Wrote: Or should I anyway have three models or Entity class and then manage the access to the three tables at Controller level?

No, it is a very bad practice.
If you could use `join` in SQL, you should use it with Query Builder.
Reply
#3

(This post was last modified: 05-21-2023, 04:56 AM by Corsari.)

Uh-Oh
Thank you for the gentle and prompt reply
I'm so sorry, I discovered right now that I had an error in my original code and (unbelievable) I have fixed it pasting and tidying the query code here in the thread
sorry again
Yes the code is fine,  I mean ,  running it , no errors (now I have wrong joins but that is another story)
Originally I had errors telling the others tables were sort of unknown that is the reason for the thread. So it was looking to me that the Model was allowing the access only to its associated table and not to 'dept' and 'post' ones.
Thank you for your overview.
P.S.
It would be great a new chapter in the guide to be called "Developing With CodeIgniter Best Practices", maybe right after the Tutorial
Anyway the guide is super.

Instead it would be great a tutorial for the new authentication applied to e.g. a blog or forum management or something like that

So with admin, staff and users
Reply




Theme © iAndrew 2016 - Forum software by © MyBB