daylightstudio/FUEL-CMS — Issue #129: get_post_count in Fuel_blog.php fails if $category is not null
Author: saltlakeryan
Created: 2012-03-09 15:31:53+00:00
State: open

get_post_count fails if the category argument is not null. My fix is below, but may not be best. It requires changes to two files:

in modules/blog/libraries/Fuel_blog.php:
function get_post_count($category = NULL)
{
$this->_CI->load->module_model(BLOG_FOLDER, 'blog_posts_model');
$tables = $this->_CI->config->item('tables');
$where[$tables['blog_posts'].'.published'] = 'yes';

if (!empty($category))
{
$where[$tables['blog_posts'].'.published'] = 'yes';
$join_where[$tables['blog_categories'].'.name'] = $category;
$cnt = $this->_CI->blog_posts_model->record_count($where, $join_where);
}
else
{
$where[$tables['blog_posts'].'.published'] = 'yes';
$cnt = $this->_CI->blog_posts_model->record_count($where);
}
return $cnt;
}

in application/core/MY_Model:

public function record_count($where = array(), $join_where = array())
{
$this->db->where($where);
$table_name = $this->table_name;
if ($join_where) {
foreach($join_where as $table_field => $match) {
list($table, $field) = explode('.', $table_field);
$table_name .= ", " . $table;
$this->db->where($table_field, $match);
}
}
$query = $this->db->get($table_name);
return $query->num_rows();
}

perhaps join_where is a misnomer for that variable since it's not really generating a table join, but rather a cross product. It looks like this is what the original code is trying to do though.

Without the above fix, get_post_count builds an illegal sql query that has a where clause for the blog_categories table, but doesn't list blog_categories in the FROM clause of the sql statement.