【问题标题】:Codeigniter - Fetch data from many tablesCodeigniter - 从许多表中获取数据
【发布时间】:2013-09-06 09:01:40
【问题描述】:

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

**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 - 我还不确定它是否需要 -)

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

所以我应该得到类似的东西 apple imac - 处理器 - intel 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

【问题讨论】:

    标签: php mysql codeigniter database-schema


    【解决方案1】:

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

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

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

    【讨论】:

    • 很抱歉我不够彻底,我已经在使用 MY_Model (codeigniter.tv)。我也尝试使用 JOINS 来执行此操作,但是当我尝试传递产品 ID 时,我没有得到任何结果。查看更新。
    【解决方案2】:

    试试:

    select p.*, f.*, pf.* from Products p, Features f, product_features pf where p.product_id = pf.product_id AND pf.feature_id = f.feature_id AND p.category_id = f.category_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 2012-03-13
      相关资源
      最近更新 更多