gpt4 book ai didi

database - Codeigniter 从模型中获取一对多关系并在 View 中进行解析

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:44 25 4
gpt4 key购买 nike

我有 3 个表,即 celebsceleb_rolesceleb_industry
celebs 是与其他 2 个表具有一对多关系的主表。

celebs 有专栏 -身份证、姓名、国籍、身份。

celebs_roles 有列 -id, celeb_id, role_id.

celebs_industry 有列 -id, celeb_id, industry_id。

角色和行业的数量有限且固定。所以我把它们放在一个带有 id=>value 对的配置文件中。因此,如果我从结果中获得 role_id,我可以从配置文件中获取值。 industry_id 也是如此。

现在为了从一个给定的 id 中获取名人的详细信息,我在模型中编写了如下查询。

public function getCelebSingle($celebId)
{
$this->db->select('*');
$this->db->from('celebs');
$this->db->join('celebs_roles', 'celebs_roles.celeb_id = celebs.id');
$this->db->join('celebs_industry', 'celebs_industry.celeb_id = celebs.id');
$this->db->where('celebs_roles.celeb_id', $celebId);
$this->db->where('celebs_industry.celeb_id', $celebId);
$query = $this->db->get();
return $query->result_array();
}

在数据库中,celebs 中有一条记录,同一个 celeb_id 在其他 2 个表中各有 2 条记录。我得到的结果如下。

Array
(
[0] => Array
(
[id] => 1
[name] => new test
[nationality] => whatever
[status] => Pending
[celeb_id] => 1
[role_id] => 1
[industry_id] => 1
)

[1] => Array
(
[id] => 1
[name] => new test
[nationality] => whatever
[status] => Pending
[celeb_id] => 1
[role_id] => 3
[industry_id] => 1
)

[2] => Array
(
[id] => 2
[name] => new test
[nationality] => whatever
[status] => Pending
[celeb_id] => 1
[role_id] => 1
[industry_id] => 2
)

[3] => Array
(
[id] => 2
[name] => new test
[nationality] => whatever
[status] => Pending
[celeb_id] => 1
[role_id] => 3
[industry_id] => 2
)
)

因此对于 celebs 表的一条记录,我需要将其放入一个循环中,我认为这有点过载。如果其他 2 个表中的记录数增加,则结果数组中的项目也会增加。所以我想知道,我能否得到如下所示的结果?

 Array
(
[0] => Array
(
[id] => 1
[name] => new test
[nationality] => whatever
[status] => Pending
[celeb_roles] => array(
[0] => Array
(
[id] => 1
[celeb_id] => 1
[role_id] => 1
)
[0] => Array
(
[id] => 1
[celeb_id] => 1
[role_id] => 2
)
)
[celeb_industry] => array(
[0] => Array
(
[id] => 1
[celeb_id] => 1
[industry_id] => 1
)
[0] => Array
(
[id] => 1
[celeb_id] => 1
[industry_id] => 2
)
)
)
)

这只是对结果数组的期望,但我不知道如何实现这个结果数组。有人可以看一下吗?

这是一个可能的副本 - Codeigniter group by and create multidimensional array .但答案并不令人满意。答案明确指出不使用 JOIN,而是使用正常的 Codeigniter get 查询,我认为这将导致对该问题进行 3 次查询。如果我选择 celebs 表的所有记录怎么办?然后,查询的数量将是 3 * Number of records in celebs table ,这将导致过载。如果没有别的办法,我只好这样做了,但我希望有更好的方法来做到这一点。

最佳答案

请这样做,我不确定这是正确的,但如果你想得到你在期望数组中声明的结果,你需要尝试这种方式。

    $this->db->select('*');
$this->db->from('celebs');
$this->db->join('celebs_roles', 'celebs_roles.celeb_id = celebs.id');
$this->db->join('celebs_industry', 'celebs_industry.celeb_id = celebs.id');
$this->db->where('celebs_roles.celeb_id', $celebId);
$this->db->where('celebs_industry.celeb_id', $celebId);
$query = $this->db->get();
$result = $query->result_array();
$new_array = array();
foreach ($result as $key => $value){
$new_array = $value;
foreach ($result as $key1 => $value1){
$new_array['celeb_roles'][$key1]['id'] = $value1['id'];
$new_array['celeb_roles'][$key1]['celeb_id'] = $value1['celeb_id'];
$new_array['celeb_roles'][$key1]['role_id'] = $value1['role_id'];
}

}

关于database - Codeigniter 从模型中获取一对多关系并在 View 中进行解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com