简体   繁体   English

Codeigniter:将activeRecord与手动查询相结合?

[英]Codeigniter: Combining activeRecord with manual queries?

I've though a bit about the activerecord vs. manual queries in Codeigniter. 我对Codeigniter中的activerecord与手动查询有点关系。 ActiveRecord is awesome when it's all about standard queries and holds development time really low. ActiveRecord非常棒,只需标准查询并且开发时间非常短。

However, when there's a need to add some complexity to the queries, the ActiveRecord gets quite complicated to work with. 但是,当需要为查询添加一些复杂性时,ActiveRecord会变得非常复杂。 Sub queries or complex joins gives atleast me a lot of headache. 子查询或复杂连接给我至少带来了很多麻烦。

Since the current "$this->db->query" -call immediately executes the set query, it can't be combined with normal activeRecord calls. 由于当前“$ this-> db-> query”-call立即执行set查询,因此无法与正常的activeRecord调用结合使用。

So, what can I do to combine the two methods? 那么,我可以做些什么来结合这两种方法呢?

Example of what I want to accomplish: 我想要完成的例子:

$this->db->select('title, content, date');
$this->db->from('mytable');
$this->db->manual('UNION'); // My own idea of db-call that appends UNION to the query
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();

Thanks! 谢谢!

maybe this link will help: active record subqueries 也许这个链接会有所帮助: 活动记录子查询

Update--- 更新---

there were another discussion about Union with Codeigniter Active Record . 关于联盟与Codeigniter Active Record的另一次讨论。 And I agree with the answer there. 我同意那里的答案。

But with some subqueries we can combine active record with manual queries. 但是对于一些子查询,我们可以将活动记录与手动查询相结合。 Example: 例:

// #1 SubQueries no.1 -------------------------------------------

$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
$subQuery1 = $this->db->_compile_select();

$this->db->_reset_select();

// #2 SubQueries no.2 -------------------------------------------

$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();
$subQuery2 = $this->db->_compile_select();

$this->db->_reset_select();

// #3 Union with Simple Manual Queries --------------------------

$this->db->query("select * from ($subQuery1 UNION $subQuery2) as unionTable");

// #3 (alternative) Union with another Active Record ------------

$this->db->from("($subQuery1 UNION $subQuery2)");
$this->db->get();

nb: sorry I haven't tested this script, hope it's works and helpfull.. nb:抱歉,我没有测试过这个脚本,希望它的工作原理和帮助。

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

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