Database Codeigniter从模型中获取一对多关系并在视图中解析

Database Codeigniter从模型中获取一对多关系并在视图中解析,database,codeigniter,model,Database,Codeigniter,Model,我有三个表,分别是-celebs,celeb\u角色,celeb\u行业 celebs是主表,与其他2个表有一对多关系 celebs拥有以下栏目- 身份证、姓名、国籍、身份 celebs\u roles具有以下列- 身份证,名人身份证,角色身份证 celebs\u industry拥有以下栏目- 身份证,名人身份证,行业身份证 角色和行业数量有限且固定。因此,我将它们放在一个配置文件中,带有id=>valuepair。因此,如果我从结果中获得role\u id,我可以从配置文件中获取值。这同样适

我有三个表,分别是-
celebs
celeb\u角色
celeb\u行业

celebs
是主表,与其他2个表有一对多关系

celebs
拥有以下栏目- 身份证、姓名、国籍、身份

celebs\u roles
具有以下列- 身份证,名人身份证,角色身份证

celebs\u industry
拥有以下栏目- 身份证,名人身份证,行业身份证

角色和行业数量有限且固定。因此,我将它们放在一个配置文件中,带有
id=>value
pair。因此,如果我从结果中获得
role\u id
,我可以从配置文件中获取值。这同样适用于
行业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条记录。 我得到的结果如下

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
表中的一条记录,我需要将其放入一个循环中,我认为这有点过载。如果其他两个表中的记录数增加,则结果数组中的项也将增加。所以我想知道,我能得到下面这样的结果吗

 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
                                )
                        )
    )
)
这只是结果数组的期望值,但我不知道如何实现这个结果数组。有人能看一下吗


这里有一个可能的副本-。但答案并不令人满意。答案清楚地表明不使用JOIN,而是使用普通的Codeigniter get查询,我认为这将导致问题的3个查询。如果我选择
celebs
表的所有记录,会怎么样?然后,查询的数量将是
3*celebs表中记录的数量
,这将过载。如果没有其他方法,我只能这样做,但我希望会有更好的方法来做。

请这样做,我不确定这是否正确,但如果您希望得到预期数组中声明的结果,则需要尝试这种方法

    $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'];
            }

    }

可能重复:非常感谢您的帮助。我试过这个。现在,经过一点修改后,celeb_角色显示在一个数组中,但我得到的不是2个celeb_角色,而是4个,因为有2个celeb_角色和2个行业角色。但我认为我可以使用array_unique并获得所需的结果。但是非常感谢你为我指路。