我正在使用CodeIgniter3。我只需要知道到目前为止是否在查询生成器中添加了“ 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();
}
答案 0 :(得分: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 );
}
答案 1 :(得分: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();
}