Reputation: 3185
Pardon the framework specific code. Please just take my word that the objects do what I say they do.
My Code
//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
}
My Question
What do I write in the else
statement to run through the code above until I find an alias that isn't being used in the Person table. My guess is some kind of loop, but I'm just not really sure how to do it. Feel free to re-work this how you like. Put it as its own function/tell me I'm doing it wrong, I won't be offended. Thank you SO!
Upvotes: 1
Views: 116
Reputation: 44161
$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*/ }
However, it may be a better idea to use an auto-incremented value for the alias-id if it does not have to be randomly generated.
To convert the number to and from base36 you can use PHP's base_convert function:
$a = base_convert(12345,10,36);
$b = base_convert($a,36,10);
print "12345 --> ".$a." --> ".$b;
output:
12345 --> 9ix --> 12345
If you want to make sure that your number is at least 5 digits start your increment value at: base_convert(10000,36,10)
= 1679616
Upvotes: 1
Reputation: 742
How about this:
$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);
Upvotes: 0
Reputation: 1546
My version with a max iteration setting (set to -1 if you want to let it run until it finds a unique)
$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!");
}
Upvotes: 0
Reputation: 3178
I would go with a while loop...
//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);
I would really look into a better way to generate the aliasId value, because once you have a lot of "person" records, you're going to be looping for a long time.
Upvotes: 0