Do you have a unique index on the field? If you haven't and you really do want 
it to be unique, I'd stick on on. Then you could work on the basis that you 
*probably* aren't going to run into the same number that often (at least it is 
far more likely that your number is unique than it is a duplicate), generate a 
random number, save it and only create another one of the save fails (then 
rinse and repeat).

Would a UUID work for you? That would save you some effort.

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 11 Aug 2010, at 10:18, Tomfox Wiranata wrote:

> ok i tried your solution and somehow cake does not care if it writes
> the same number over and over in my database. maybe you see what's
> wrong here :)
> 
> my function in my model "abc" that finds and compares the number. my
> logic: when no entry is found here $eyed is empty.
> 
>    function validateEyed($data)
>    {
>        $eyed = $this->find(array('eyed' => $data), array('eyed'));
>        if(empty($eyed) == true)
>            return $eyed['abc'];
>        return false;
>    }
> 
> 
> I am calling this function from my controller
> 
>    function __createEyed()
>    {
> 
>       do{
>               $test=rand(1,2);
>       }
>       while ($eyed = $this->abc->validateEyed($test) == true);
>       return $test;
>    }
> 
> 
> cakes writes those rand numbers "1" or "2" multiple times in my table.
> so where is the mistake??
> 
> thx :)
> 
> 
> On 10 Aug., 18:39, cricket <zijn.digi...@gmail.com> wrote:
>> On Tue, Aug 10, 2010 at 12:30 PM, Tomfox Wiranata
>> 
>> <tomfox.wiran...@gmail.com> wrote:
>>> thx to both..
>>>  :)
>> 
>>> @cricket:
>> 
>>> am i getting it right? you are writing all numers from the database
>>> into an array and compare both? is it possible that at a certain point
>>> it will get to big or too much data? cause my range starts at
>>> 111111111 and ends at 999999999
>> 
>>> if there are 50million in the table they all get saved in an array....
>> 
>> Yes, that's correct. I should have pointed that out. You could cache
>> the results, of course, but perhaps a better way would be to pass
>> $new_number to a protected method (that does a find() and comparison)
>> inside the while section. You'd have to have the separate method
>> because while() isn't a block, where you can have several lines of
>> code. Something like:
>> 
>> public function getNewNumber()
>> {
>>         do
>>         {
>>                 $new_number = rand(10,100000);
>>         }
>>         while ($this->_testNewNumber($new_number));
>> 
>>         return $new_number;
>> 
>> }
>> 
>> protected function _testNewNumber($x)
>> {
>>         // return $x exists in DB
>> 
>> }
> 
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
> 
> You received this message because you are subscribed to the Google Groups 
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to