2014-06-07 66 views
0

我正在處理樹形結構關係 - 向後。這意味着,我有數據庫中的其他用戶引用該網站的用戶,並且我需要檢索高達10個級別的上級引薦人。

我在我的函數裏面使用了匿名函數,但是它說「最大函數嵌套級別達到了'100',正在中止!」我在這裏做錯了什麼?

這裏是我的代碼:匿名函數遞歸不返回指定的計數級別

public function showSponsors($user_id, $count = 10) 
{ 
    //The array to store referrers 
    $Referrers = array();  
    //The anonymous function that uses Laravel Eloquent model to retrieve data 
    $GetReferrers = function($id) use(&$Referrers, &$count, &$GetReferrers) 
    {   
     //Get the first referrer and store id as index and name as value    
     $Referrers[$upper = User::find($id)->referrer] = User::find($upper)->Name; 

     //Make sure you do not return more than $count 
     //Make sure the referrer returned has a referrer to return 
     if(count($Referrers) < $count || !empty($upper || $upper != $user_id)){ 
      $count--; return $GetReferrers($upper); 

     } 
     //Otherwise return the referrers; 
     return $Referrers; 
    }; 

    //Run the function using the user id 
    $GetReferrers($user_id); 

    //return the results stored in the Referrers array  
    return $Referrers; 
+0

http://stackoverflow.com/questions/4293775/increasing-nesting-functions-calls-limit – Ashalynd

+0

問題是,它應該只能運行10個級別,爲什麼ti達到100以上?我應該在哪裏減少它?我在上面編輯的代碼中嘗試過...不行!問題不在於增加限制達到最高水平達成上面有人建議... – JiggaJitsu

+0

我想你應該調用你的$ GetReferrers爲$ GetReferrers($ upper,$ count)然後。它不能訪問嵌套調用中的原始$ count變量。 – Ashalynd

回答

0

我想你應該打電話給你$ GetReferrers爲$ GetReferrers($上,$計數)即可。它不能訪問嵌套調用中的原始$ count變量。