CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

The count_row function counts with deleted rows.

Open emrahoruc opened this issue 9 years ago • 1 comments

$this->soft_deletes = true;

This is not affect the count_row function.

Quick fix:

public function count_rows($where = NULL)`

    {
        if(isset($where))
        {
            $this->where($where);
        }

        /*add this*/
        elseif($this->soft_deletes===TRUE)
        {
            $this->_where_trashed();
        }

        $this->_database->from($this->table);
        $number_rows = $this->_database->count_all_results();
        return $number_rows;
    }

emrahoruc avatar Aug 18 '16 13:08 emrahoruc

As the criteria to include or exclude trashed records is always evaluated it the where method. Make a call to the where method without any arguments and it will exclude the trashed records.

$count = $this->some_model->where()->count_rows();
//or if you have another where clause
$count = $this->some_model->where('some_field_name','value')->count_rows();

Both will exclude the trashed records.

salain avatar Oct 17 '16 09:10 salain