How to access more than one table in the same model - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: How to access more than one table in the same model (/showthread.php?tid=87684) |
How to access more than one table in the same model - Corsari - 05-19-2023 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() 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 RE: How to access more than one table in the same model - kenjis - 05-19-2023 (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. RE: How to access more than one table in the same model - Corsari - 05-21-2023 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 |