加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:03:36 所属栏目:PHP教程 来源:网络整理
导读:我对Codeigniter中的activerecord与手动查询有点关系. ActiveRecord非常棒,只需标准查询并且开发时间非常短. 但是,当需要为查询添加一些复杂性时,ActiveRecord会变得非常复杂.子查询或复杂连接给我至少带来了很多麻烦. 由于当前的“$this- db- query”-call
我对Codeigniter中的activerecord与手动查询有点关系. ActiveRecord非常棒,只需标准查询并且开发时间非常短.

但是,当需要为查询添加一些复杂性时,ActiveRecord会变得非常复杂.子查询或复杂连接给我至少带来了很多麻烦.

由于当前的“$this-> db-> query”-call会立即执行set查询,因此无法与正常的activeRecord调用结合使用.

那么,我可以做些什么来结合这两种方法呢?

我想要完成的例子:

$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,date');
$this->db->from('mytable2');
$query = $this->db->get();

谢谢!

解决方法

也许这个链接会有所帮助: active record subqueries

更新—

还有另一个关于Union with Codeigniter Active Record的讨论.我同意那里的答案.

但是对于一些子查询,我们可以将活动记录与手动查询相结合.例:

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

$this->db->select('title,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,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:抱歉,我没有测试过这个脚本,希望它的工作原理和帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读