简体   繁体   中英

Codeigniter - how to use the foreign keys from one table to look for the id's of another table

I'm having a problem trying to wrap my head around how to query the database to retrieve the foreign keys in my Nav table and then using those to find the id's in the NavItem table. Can someone point me in the right direction or give me some pointers? i'm not even sure if this should be done in the model or the controller!

Thanks in advance

EDIT

My NavItem table looks like this:

id, button1, button2, button3, button4, region. (the buttons are all foreign keys leading to a separate buttons table)

You have to either query the buttons table four times, once for each of button1-button4, or you have to join the button table four times in your select from NavItem.

In the former case, your model would have two functions - a select from NavItem, and a select from buttons. You call the select from NavItem function from your controller, and use the data it returns to run the select from buttons function four times, also from the controller.

In the latter case, you only have to have one model function that runs one query, albeit not a trivial one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM