我在category
模型中创建了一个函数:
public function getSecondCategoryByID($category_id = 0,$id = 0)
{
$category = $this->where('id','=',$category_id)->first();
if($category->parent_cat_id == 1)
{
$mcategory = $this->where('id','=',$id)->first();
echo 'Inside Model '.$mcategory->id;
return $mcategory->id;
}else{
$this->getSecondCategoryByID($category->parent_cat_id,$category->id);
}
}
在控制器的一个功能上我调用了这个函数:
$category = new Category;
$secondCategoryID = $category->getSecondCategoryByID($category_brand->category_id,0);
echo 'After Model '.$secondCategoryID ;
return ;
当我调用我的控制器功能时,我在页面上收到了这条消息:
Inside Model 3After Model //it should be Inside Model 3After Model '3'
我认为我的模特没有任何回报!因为after model
之后什么都没有!!!!!
答案 0 :(得分:2)
在进行递归时,您还需要返回递归调用,这里通过更改else块语句来显示。
else
{
return $this->getSecondCategoryByID($category->parent_cat_id,$category->id);
}