Search code examples
activerecordcodeigniter-2

Active record of not in


 $this->db->query('DELETE FROM default_model WHERE cat_id NOT IN (SELECT id FROM default_category)');

the active record format for this query

$query = $this->db->select('id')
                              ->get('category')->result();

                $this->db->where_not_in('cat_id',$query )
                        ->delete('model');

I tried a lot but could not do it. How to pass $query


Solution

  • The query results are returned as standard objects. Therefore you need to fetch the ids from the returned objects in a non associative array before passing that array to the delete query.

    Like this:

    $result = $this->db->select('id')->get('category')->result();
    $ids = [];
    foreach($result as $result)
         $ids[] = $result->id;
    
    $this->db->where_not_in('cat_id', $ids)->delete('model');