php - Codeigniter - 从许多表中获取数据

标签 php mysql codeigniter database-schema

您好,我需要一些帮助。 我有3张 table

**Products**
product_id //eg 1
product_name //eg apple imac
description // mplah mplah
category_id // 3 - desktops
...

**Features**
feature_id // 1
feature_name // processor
category_id // takes the parent-category eg computers with id = 1

and a pivot table **product_features** that should have something like this
product_id // 1
feature_id // 1
feature_value // intel i5

每个表还代表一个模型(所以我有一个product_model、feature_model 和一个product_feature_model - 我还不确定是否需要 -)

在我看来,我有一个进行插入/编辑的表单,我想在其中获取特定类别(在我的示例中为“计算机”)的所有功能以及也属于特定产品的值。

所以我应该得到类似的东西 苹果 imac - 处理器 - 英特尔 i5尼康 - 变焦 - 12Mpx

我怎样才能进行这个查询以及最好放入哪个模型?

这是我在 phpmyadmin 中测试过的查询

SELECT `features`.*, `product_features`.`value`
FROM (`features`)
LEFT JOIN `product_features` ON `features`.`feature_id`=`product_features`.`feature_id`
WHERE `features`.`category_id` =  1
WHERE `product`.`product_id` = 1 // without this, it returns  feature_id, feature_name, category_id and feature_value collumns, but I also want to specify from wich product

最佳答案

1)产品是你的“主要”实体,所以我会把它放在那里

2) 对所有模型使用 MY_Model 基本模型(您可以在 Google 上搜索 CodeIgniter)

3) 如果您执行 2,您将需要连接表模型,以便可以进行简单的更新

关于php - Codeigniter - 从许多表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18653869/

相关文章:

javascript - 如何满足网站上的 HTTP 和 HTTPS 请求?

php - MySQL 事务 - 更新 2 个表

.htaccess - 如何在 XAMPP 上使用 Codeigniter 删除 index.php?

Codeigniter 4 查询生成器连接 2 个或更多表而没有重复结果

php - 使用 PHP 的 Spotify 的 RSS 提要

php - 如何解决问题 - 请安装 "intl"扩展以获得完整的本地化功能

php - 无法使用 fancybox 输入 CKeditor

mysql - SQL 中的分组或 Sequelize 中的 where check 和 where in count 以及

php - SQLSTATE[HY000] [1045] 用户 mimi_oboss 访问被拒绝

php - CodeIgniter form_validation 没有显示错误