CI:查询模型中的两个表,爆炸

发布于 2024-10-05 18:51:48 字数 1951 浏览 0 评论 0原文

我已经考虑这个问题好几天了,但没有掌握(因为我对 MVC 和 CI 相对陌生)。我什至不确定这是 MVC、MySQL 还是数组的问题。

情况:2 个 MySQL 表

  1. 数据:id、标题、列表
  2. :id、名称

查询数据表结果如下所示(摘录):

[4] => Array
        (
            [id] => 3
            [title] => Foo                
            [list] => 1,2,3,4,6,14
        )

[5] => Array
        (
            [id] => 4
            [title] => Bar                
            [list] => 2,6,9,12
        )

字段 list 包含与 values 表的某些 ID 相对应的逗号分隔值,例如

[3] => Array
            (
                [id] => 12
                [name] => 'value12'                
            )

我尝试为每个执行的操作行是:

  • 列表-值&将其分解为数组,
  • 检查表中的结果集(通过 in_array() 方法),
  • 则返回 ID 的名称值
  • 如果

    以某种方式将其包含到主结果中, 设置(例如作为二维数组):

    [5] =>大批 ( [id] => 4 [标题] =>酒吧 [列表] =>大批 ( [0] =>值6 [1] =>值12 ... ) )

到目前为止,我的天真的方法是

  • 对 2 个表中的每一个运行查询,
  • 通过 in_array 比较 2 个结果集

我的主要问题(同时尝试严格分离模型、控制器和视图):如何从值中包含名称字段-数据表结果集的“主循环”中的表?

if($q->num_rows() > 0)
{
    $data[] = $q->result_array();
    foreach ($q->result() as $row)
    {
        $data[] = $row;
    }
    return $data;
}

如果我使用以下(麻烦的)方法,我自然每次都会得到一个新项目:

foreach ($q->result_array() as $row) 
{
    $data[]['id'] = $row['id'];
    $data[]['title'] = $row['title'];
    $data[]['list'] = $row['year'];
}

由于这是一个 MySQL 数据库,我看不出有什么办法可以在 SQL 中进行爆炸和比较(使用 LIKE 或其他方法)。

任何提示,即使是一个简单的信息链接,都将受到高度赞赏。

感谢一万亿!

很棒的

i'm thinking about this for days now and don't come to grasps (since i'm relativley new to MVC and CI). I'm not even sure whether this is an issue with MVC, MySQL or arrays.

Situation: 2 MySQL tables

  1. Table data: id, title, list
  2. Table values: id, name

Querying the data table results in an array like the following (excerpt):

[4] => Array
        (
            [id] => 3
            [title] => Foo                
            [list] => 1,2,3,4,6,14
        )

[5] => Array
        (
            [id] => 4
            [title] => Bar                
            [list] => 2,6,9,12
        )

The field list contains comma separated values that correspond to some IDs of the values table like

[3] => Array
            (
                [id] => 12
                [name] => 'value12'                
            )

What I try to do for each row is:

  • take the list-values & explode it into an array
  • check with the result set from the values-table (via in_array() method)
  • return the name values of the IDs if
  • include it somehow into the main result set (e.g. as a 2-dimensional array):

    [5] => Array (
    [id] => 4
    [title] => Bar
    [list] => Array (
    [0] => value6
    [1] => value12
    ...
    )
    )

My naive approach so far was to

  • run a query on each of the 2 tables
  • compare the 2 result sets via in_array

My main problem (while trying to strictly separate model, controller and view): How can I include the name field from the values-table in the "main loop" of the data table result set?

if($q->num_rows() > 0)
{
    $data[] = $q->result_array();
    foreach ($q->result() as $row)
    {
        $data[] = $row;
    }
    return $data;
}

If I use the following (cumbersome) approach i naturally get a new item each time:

foreach ($q->result_array() as $row) 
{
    $data[]['id'] = $row['id'];
    $data[]['title'] = $row['title'];
    $data[]['list'] = $row['year'];
}

Since this is a MySQL database I see no way to do the explode and the comparison in SQL (with LIKE or something else).

Any hint, even a simple link to an info bit, is highly appreciated.

Thanks a trillion!

fab

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-10-12 18:51:49

列表和列表值之间存在多对多关系。在关系数据库中对此进行建模的传统方法是创建连接表。所以我会像这样构建你的架构。

lists : list_id, title
values : value_id, name
list_values : list_id, value_id

list_values 是连接表。它将列表与值链接起来。

要构建列表,您可以在模型中使用以下函数

function build_list($list_id)
{
    $list = $this->get_list($list_id);
    $list->values = $this->get_list_values($list_id);
    return $list;
}

function get_list($list_id)
{
    $sql = 'select * from lists where list_id=?';
    return $this->db->query($sql, array($list_id))->row();
}

function get_list_values($list_id)
{
    $sql = 'select v.value_id, v.name
        from list_values lv
        join values v on v.value_id=lv.value_id
        where lv.list_id=?';
    return $this->db->query($sql, array($list_id))->result();
}

There is a many-to-many relationship between lists and list values. The conventional way to model this in a relational database is to create a joining table. So I'd structure your schema like this.

lists : list_id, title
values : value_id, name
list_values : list_id, value_id

list_values is the joining table. It links lists with values.

To build a list you could have the following functions in your model

function build_list($list_id)
{
    $list = $this->get_list($list_id);
    $list->values = $this->get_list_values($list_id);
    return $list;
}

function get_list($list_id)
{
    $sql = 'select * from lists where list_id=?';
    return $this->db->query($sql, array($list_id))->row();
}

function get_list_values($list_id)
{
    $sql = 'select v.value_id, v.name
        from list_values lv
        join values v on v.value_id=lv.value_id
        where lv.list_id=?';
    return $this->db->query($sql, array($list_id))->result();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文