Codeigniter 3: Есть ли какое-либо условие «где» добавлено в Query Builder? - PullRequest
0 голосов
/ 10 марта 2019

Я использую CodeIgniter 3. Мне просто нужно знать, есть ли условие 'где', добавленное в конструктор запросов до сих пор.

Я вызываю функцию '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();
} 

Ответы [ 2 ]

0 голосов
/ 10 марта 2019

Я нашел решение. Единственное, что мне нужно сделать, это получить запрос на выборку и найти в нем предложение '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();
} 
0 голосов
/ 10 марта 2019

В контроллере

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 );
} 
...