简体   繁体   English

mysql到codeigniter活动记录帮助

[英]mysql to codeigniter active record help

Active record is a neet concept but sometimes I find it difficult to get more complicated queries to work. 活动记录是一个新概念,但有时我发现很难使更复杂的查询生效。 I find this is at least one place the CI docs are lacking. 我发现这至少是CI文档所缺少的一个地方。

Anyway, This is the sql I wrote. 无论如何,这是我写的sql。 It returns the expected results of quests not yet completed by the user that are unlocked and within the users level requirements: 它返回用户尚未完成并在用户级别要求内的尚未完成的任务的预期结果:

SELECT writing_quests . * 
FROM  `writing_quests` 
LEFT OUTER JOIN members_quests_completed ON members_quests_completed.quest_id = writing_quests.id
LEFT OUTER JOIN members ON members.id = $user_id
WHERE writing_quests.unlocked =1
AND writing_quests.level_required <= $userlevel
AND members_quests_completed.user_id IS NULL 

This is the codeigniter active record query, it returns all quests that are unlocked and within the users level requirement: 这是codeigniter活动记录查询,它返回所有解锁的并且在用户级别要求内的任务:

$this->db->select('writing_quests.*');
$this->db->from('writing_quests');
$this->db->join('members_quests_completed', 'members_quests_completed.quest_id = writing_quests.id', 'left outer');
$this->db->join('members', "members.id = $user_id", 'left outer');
$this->db->where('writing_quests.unlock', 1);
$this->db->where('writing_quests.level_required <=', $userlevel);   
$this->db->where('members_quests_completed.user_id is null', null, true);

I'm guessing there is something wrong with the way I am asking for Nulls. 我猜我要求Null的方式有问题。 To be thorough, I figured I'd include everything. 确切地说,我认为我会包括所有内容。

$this->db->where('column IS NOT NULL')

在你的情况下

$this->db->where('members_quests_completed.user_id is not null');

I agree that sometimes CI active record can overcomplicate things. 我同意有时CI活动记录可能会使事情复杂化。 Try this for your IS NULL where clause: 为您的IS NULL where子句尝试以下操作:

$this->db->where('members_quests_completed.user_id IS ','NULL',false);

Try also to enable the profiler or echo the generated query with: 也尝试启用分析器或使用以下命令回显生成的查询:

echo $this->db->last_query();

That might shed some light on what the issue is. 这可能会阐明问题所在。

Sometimes CI makes it a bit complicated... you can always use $this->db->query("your very long query") to simplify a bit (if I recall correctly strings are still escaped - not sure). 有时CI会使它变得有点复杂...您可以始终使用$ this-> db-> query(“您的很长的查询”)进行简化(如果我没记错的话,字符串仍然会转义-不确定)。 It's a personal opinion. 这是个人观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM