Codeigniter,活动记录,加入

Codeigniter, active record, join

我目前正在制作一个小游戏来学习一些活动记录 (codeigniter) 和 php。所以我对这一切都很陌生。

当然,我在路上遇到了以下挑战。

挑战是,我有两个 table,User_HeroesHeroes。 table Heroes 包含用户在玩游戏期间可以选择的所有英雄(属于他的派系)和 User_Heroes 是用户已经招募到他的服务中的 table 个英雄。

我想做的事情如下,我想查询return当前用户没有雇佣的英雄列表。

为了简单地得到它们,我会这样做

public function get_faction_heroes($sUser, $hero_faction){
        $oQuery = $this -> db
                        -> select(' id,
                                    hero_name,
                                    hero_faction,
                                    hero_type,
                                    hero_cost,
                                    hero_maintenance,
                                    hero_allowed')
                        -> like('hero_faction',$hero_faction)
                        -> get('heroes')
                        -> result();

        return $oQuery;
    }

目前我建立了以下查询,但显然它是错误的,因为它没有 return 我期望的列表 return。

 public function get_faction_heroes($sUser, $hero_faction){
    $oQuery = $this -> db
                    -> select(' h.id,
                                h.hero_name,
                                h.hero_faction,
                                h.hero_type,
                                h.hero_cost,
                                h.hero_maintenance,
                                h.hero_allowed')
                    -> like('h.hero_faction',$hero_faction)
                    -> where_not_in('u.hero_id', $sUser)
                    -> where('h.hero_allowed >', 1)
                    -> join('user_heroes u', 'u.hero_id = h.id', 'left')
                    -> get('heroes h')
                    -> result();

    return $oQuery;
}

您可能已经注意到,某些英雄可以被多次招募 (hero_allowed > 1)。

也许是为了进一步清理 table 我添加了两张当前 tables

的图片

$this->db->select('*')->from('Heroes');
$this->db->where('`id` NOT IN (SELECT `hero_id` FROM `User_Heroes`)', NULL, FALSE);
$query=$this->db->get();
return $query->result();