简体   繁体   English

Codeigniter-如何使用一个表中的外键查找另一表的ID

[英]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. 我在尝试如何查询数据库以检索Nav表中的外键,然后使用这些键在NavItem表中查找id时遇到问题。 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: 我的NavItem表如下所示:

id, button1, button2, button3, button4, region. id,button1,button2,button3,button4,区域。 (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. 您必须查询按钮表四次,每个按钮1-button4一次,或者必须在NavItem的选择中联接按钮表四次。

In the former case, your model would have two functions - a select from NavItem, and a select from buttons. 在前一种情况下,模型将具有两个功能-从NavItem中进行选择,以及从按钮中进行选择。 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. 您从控制器调用NavItem函数中的select,并使用它返回的数据运行四次(也从控制器)按钮选择中的功能。

In the latter case, you only have to have one model function that runs one query, albeit not a trivial one. 在后一种情况下,您只需要具有一个模型函数即可运行一个查询,尽管它不是琐碎的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Codeigniter联接将ID从一个表的ID更改为另一个表的ID - codeigniter join changing id from one table's id to another 如何在一个表中使用多个外键引用 cakephp 中的另一个表 - How do I use multiple foreign keys in one table referencing another table in cakephp 如何将一张表的所有id插入另一张表 - How to insert all id's from one table to another table 如何基于另一个表ID Codeigniter更新一个表 - how to update one table based on another table ID codeigniter MySQL:一个表中的两个外键引用另一个表 - MySQL: Two foreign keys in one table referring to another table 从一个表中获取具有特定ID的列的总和,并在Codeigniter中从具有该ID的另一张表中获取详细信息 - Get the sum of the colunm with specific id from one table and get the details from another table with that id in codeigniter Codeigniter:如何使用外键ID从外表获取不同的列数据? - Codeigniter : How to fetch different column data from foreign table using foreign key id? 如何将一个表中的Id与另一个表相关联 - How to link Id from one table with another 使用一个表中的值作为另一个表中的键 (PHP) - Use values from one table as keys in another (PHP) 使用外键将数据从一个表插入到另一个表 - inserting data from one table into another from drop-down using foreign keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM