Codeigniter 3:到目前为止,是否有任何 'where' 条件添加到查询生成器?

Codeigniter 3: Is there any 'where' condition added to the Query Builder so far?

我正在使用 CodeIgniter 3。我只需要知道到目前为止是否有 'where' 条件添加到查询构建器。

我正在调用一个从数据库中删除行的 'delete' 函数,并且可以在调用该函数之前添加一个 where 条件。像这样:

public function delete()
{
    // Here I need to know if where condition added to the db class

    $this->db
        ->where('field', 1)
        ->delete('my_table');
}

public function main()
{
    $this->db->where('field2', 2);
    $this->delete();
} 

在控制器中

function delete_row()
{
   $this->load->model('model_name');

   // Pass the id or something else to the row_delete() method
   $this->model_name->row_delete($id);
}

模型中

function row_delete($id)
{
   $this->db->where('id', $id);
   $this->db->delete('table_name'); 
}

根据你的例子:

public function delete_method($condition,$table_name )
{

    $this->db->where($condition)
    $this->db->delete($table_name);
}

public function main()
{
   $condition = [
     'field1'=>1,
     'field2'=>2
   ];

   $table_name = 'my_table';
   $this->delete_method($condition,$table_name );
} 

我找到了解决办法。 我唯一需要做的就是获取 select 查询并在其中搜索 'where' 子句:

public function delete()
{
    // Here I need to know if where condition added to the db class

    $sql = $this->db->get_compiled_select(NULL, FALSE);
    $has_where = stripos($sql, 'where') !== FALSE;

    // $has_where is TRUE if there is a where condition defined until now

    $this->db
        ->where('field', 1)
        ->delete('my_table');
}

public function main()
{
    $this->db->where('field2', 2);
    $this->delete();
}