2011-06-29 184 views
1

赦免框架特定的代碼。請聽我的話,這些物體按我所說的做。php循環問題

我的代碼

 //Generates a random five digit alphanumerical id 
     $aliasId = $model->random_id_gen('5'); 

     //calls the active record class for table Person      
     $person = new Person(); 

     //searches the person table to see if this alias is being used   
     $search = $person->find('alias=:alias', array(':alias'=>$aliasId)); 

     //if null then it sets an attribute for a another active record class      
     if ($search==NULL) 
     { 
       $model->setAttribute('alias', $aliasId); 
       $model->setIsNewRecord(TRUE); 
     } 
     else 
     { 
     //I need to loop through the above code until I find an alias that isn't being used        
     } 

我的問題

我怎麼在else聲明通過代碼,直到我發現,沒有在使用一個別名運行上面寫人表。我的猜測是某種循環,但我不確定如何去做。隨意重新工作,你如何喜歡。把它作爲自己的功能/告訴我我做錯了,我不會被冒犯。謝謝你這麼!

+0

確實$ aliasId必須是隨機的(如果你要讓它運行,直到它找到一個獨特的設置爲-1)?如果您使用自動生成的順序值,這可能會更加高效。 –

+0

它需要是字母數字,哪些mysql不會 –

+0

一個好主意,但最終會增加數量。我需要它是五位數。 –

回答

1
$found = false; 
$iter = 0;  // It's a good idea to include an upper bound on the number of iterations 
while(!$found && $iter < 1000){ 
    $aliasId = $model->random_id_gen('5'); 

    //calls the active record class for table Person      
    $person = new Person(); 

    //searches the person table to see if this alias is being used   
    $search = $person->find('alias=:alias', array(':alias'=>$aliasId)); 

    //if null then it sets an attribute for a another active record class      
    if (is_null($search)){ 
     $model->setAttribute('alias', $aliasId); 
     $model->setIsNewRecord(TRUE); 
     $found = true; 
    } 
    $iter++; 
} 

if(!$found){ /* Some error condition because a suitable ID could not be found*/ } 

但是,如果不必隨機生成,那麼對alias-id使用自動遞增的值可能會更好。

將數字轉換和從base36您可以使用PHP的base_convert功能:

$a = base_convert(12345,10,36); 
$b = base_convert($a,36,10); 
print "12345 --> ".$a." --> ".$b; 

輸出:

12345 --> 9ix --> 12345 

如果你想確保你的數量至少是5個位數啓動您的增量值爲:base_convert(10000,36,10) = 1679616

+0

我不知道MySQL做字母數字自動遞增? –

+0

@k到z:您總是可以選擇最大的字母數字值,將其轉換爲整數,然後將其加1並將其轉換回來。它可能會比反覆生成一個隨機字符串更有效,直到找到一個字符串(尤其是如果您的數據庫開始累積大量條目)。 – GWW

+0

這可能是最好的解決方案。任何方式,你可以通過將字母數字值轉換爲整數並回到你的答案來顯示你的意思嗎?確保ID保持5位數字的關鍵在於此。 –

0

這個怎麼樣:

$search = true; //set to a non-null value 
while (!is_null($search)) { 
    $aliasId = $model->random_id_gen('5'); 

    //calls the active record class for table Person      
    $person = new Person(); 

    //searches the person table to see if this alias is being used   
    $search = $person->find('alias=:alias', array(':alias'=>$aliasId)); 
} 
$model->setAttribute('alias', $aliasId); 
$model->setIsNewRecord(TRUE); 
+0

對於NULL值,您應該使用is_null而不是!==。 – GWW

+0

@GWW感謝那 – JoshB

+0

@GWW,更高效還是最好的做法還是兩者兼而有之?附:我正在等待這些答案,然後再標記出正確答案。 –

0

我會去與while循環...

//calls the active record class for table Person      
$person = new Person(); 

//Generates a random five digit alphanumerical id 
$aliasId = $model->random_id_gen('5'); 

while ($person->find('alias=:alias', array(':alias'=>$aliasId))) 
{ 
    $aliasId = $model->random_id_gen('5'); 
} 
//sets an attribute for a another active record class      
$model->setAttribute('alias', $aliasId); 
$model->setIsNewRecord(TRUE); 

我真的尋找到一個更好的方式來產生aliasId價值,因爲一旦你有很多的「人」的記錄,你將會持續很長時間。

+0

那是在錯誤的地方。 id在循環之間不會改變 – JoshB

+0

@joshb我認爲它看起來很好。 – dimi

+0

@dbalaouras對不起,你是對的。我誤解了代碼。 – JoshB

0

我用最大迭代設置版本

$person = new Person(); 
$unique = false; 
$maxloops = 100; 
while($maxloops>=0){ 
    //Generates a random five digit alphanumerical id 
    $aliasId = $model->random_id_gen('5'); 
    //searches the person table to see if this alias is being used 
    $unique = empty($person->find('alias=:alias', array(':alias'=>$aliasId)));   
    if($unique) break; 
    $maxloops--; 
} 

if($unique){ 
    $model->setAttribute('alias', $aliasId); 
    $model->setIsNewRecord(TRUE); 
}else{ 
    trigger_error("oops!"); 
}