0

我正在使用 CodeIgniter 3。我只需要知道到目前为止是否有一个“where”条件添加到查询生成器中。

我正在调用从数据库中删除行的“删除”函数,并且可以在调用该函数之前添加 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();
} 
4

2 回答 2

0

在控制器中

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 );
} 
于 2019-03-10T07:19:56.510 回答
0

我找到了解决方案。我唯一需要做的就是获取一个选择查询并在其中搜索“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();
} 
于 2019-03-10T07:53:51.257 回答