Php 清除CodeIgniter中的选择字段
我使用一个函数来设置整个sql查询。 其他两个使用此函数获取sql,然后根据需要使用它 现在,我需要另一个函数来执行相同的查询,但我不需要前面函数所需的所有字段。因此,我想知道是否有一种方法可以在设置查询的SELECT部分后替换它 伪代码:Php 清除CodeIgniter中的选择字段,php,mysql,codeigniter,select,Php,Mysql,Codeigniter,Select,我使用一个函数来设置整个sql查询。 其他两个使用此函数获取sql,然后根据需要使用它 现在,我需要另一个函数来执行相同的查询,但我不需要前面函数所需的所有字段。因此,我想知道是否有一种方法可以在设置查询的SELECT部分后替换它 伪代码: function sql(){ $this->db->select('*'); $this->db->from('table'); } function defaultSql(){ $this->sql(); $
function sql(){
$this->db->select('*');
$this->db->from('table');
}
function defaultSql(){
$this->sql();
$this->get();
}
function notAllFields(){
$this->sql();
// This is the solution I'm looking for. It should clear the select
$this->db->clearSelect();
// I define a different select
$this->db->select('field');
$this->get();
}
提前谢谢 你能这样试试吗
function sql($fields = '*') {
$this->db->select($fields);
$this->db->from('table');
}
function defaultSql() {
$this->sql();
// ......
}
function notAllFields() {
$this->sql('field');
// .....
}
这里有一个这样做的简单方法 假设您正在模型中编写所有这些函数
function sql($status = false)
{
if($status == true){
$this->db->select('field');
}else{
$this->db->select('*');
}
}
function get()
{
$this->db->from('table');
}
function defaultSql(){
$this->sql();
$this->get();
}
function notAllFields(){
$this->sql(true);
$this->get();
}
您可以通过选择重置数据库
1- <?php $this->db->clear_select(); ?>
这将重置所有查询变量select、where、join..from..等
2-通过创建MY_DB_mysqli_driver.php在应用程序/库中扩展数据库驱动程序,该驱动程序应包含以下内容
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_DB_mysqli_driver extends CI_DB_mysqli_driver
{
public function __construct($param)
{
parent::__construct($param);
}
public function clear_select()
{
$this->qb_select = array();
}
}
然后您可以使用$this->db->select\u clear函数只清除select部分。是的,这可能是一种获得相同结果的方法,但它不会清除select部分。与DBK相同。这是一种获得与清除选择部分相同结果的方法,但这不是我想要的。@j4nsol为什么在选择后还要清除选择部分呢。当你能用条件处理事情的时候,你为什么要这么做呢?注意,我正在寻找一个解决方案,它实际上会清除一个已经设置好的选择集。我的源代码比示例复杂得多,因此我不想向sql函数传递任何参数;