代码点火器行为异常:主控制器中的函数按预期工作,但在辅助程序中返回空数组



我有以下函数,它从表中检索父类别,然后将其保存到数组中,直到没有人找到。 当直接从 URL 浏览器在主控制器中调用此函数时,此函数工作正常,但是当从 codeigniter 帮助程序调用它并馈送到 count() 函数时,返回没有元素的空数组。

// in codeigniter helper:
function get_category_ancestors($catId)
{
$ci =& get_instance();
static $categoryAncestors = array();
$category = $ci->a_model->get_cat_parent($catId);
if($category->num_rows() > 0)
{
$categoryParent = $category->row_array();
$categoryAncestors[][$categoryParent["id"]] = $categoryParent["categoryName"];
get_category_ancestors($categoryParent["id"]);
}
else
{
$inOrder = array_reverse($categoryAncestors);
return $inOrder;
}
}
// model: (for clear demonstration)
// two table: category and category_relation
public function get_cat_parent($categoryId)
{
$this->db->select('category.*' );
$this->db->from('category_relation');
$this->db->where('category_relation.subCategoryId', $categoryId);
$this->db->join('category', 'category.id=category_relation.categoryId');
$query = $this->db->get();
return $query;
}
// some other helper:(get_category_ancestors() caller)
$categoriesInOrder = get_category_ancestors($categoryId);   
$totallAncestors = count($categoriesInOrder); // =>error: Parameter must be an array or an object that implements Countable

必须将get_category_ancestors返回到工作中。return get_category_ancestors($categoryParent["id"]);不仅仅是get_category_ancestors($categoryParent["id"]);.

最新更新