Preface: I'm using Zend Framework 1.12 and RedBean PHP.
The function below queries the table to see if any entries exist. If true, it strips the numerical value from the filename, increments it, then returns it. Pretty simple. If no entries exist, start at 1. Again, basic. This all works as intended, except for 2 predictably random moments. I say predictably random because I've successfully replicated the issue several times and each time the result is similar, but different.
The predictable part is that the numerical value will seemingly duplicate twice while the counter is <10 (except 1 out of 10 replications). I can continue to increment the function until the counter reaches 60 without further dupes. There is no pattern to which number gets duplicated either.
Below is the function using ZF and RedBean and following that is the output.
public function getNextOrderFormNumber() {
$orderFormNumber = R::getCell( 'select orderFormFilename from orders_manufOrderForms order by dateCreated desc limit 1' );
// if an entry exits
// the result will be like 'mmOrderForm-1.pdf'
if($orderFormNumber){
$num = explode('.', substr($orderFormNumber, 12));
$this->orderFormNumber = $num[0];
}
Zend_Debug::dump($orderFormNumber, 'DB');
Zend_Debug::dump($this->orderFormNumber, 'Current Number');
Zend_Debug::dump( date('i:s') );
// start at 1 for first order, else increment the value
if(!$this->orderFormNumber) {
$this->orderFormNumber = 1;
} else {
$this->orderFormNumber++;
}
Zend_Debug::dump($this->orderFormNumber, 'newOrderNumber');
return $this->orderFormNumber;
}
If you're still following, I have several ZF dumps in there to double check the output to verify what was actually happening. I also added a timestamp because I thought for whatever reason it was a timing issue, such that the script was lagging, but seems not to be the case. I should also add that it's not just a random duplicate as in something is stuck, it is actually executing a call, but just overwriting the previous value.
DB array(0) {
}
Current Number NULL
string(5) "26:29"
newOrderNumber int(1)
filename to save as string(17) "mmOrderForm-1.pdf"
--
DB string(17) "mmOrderForm-1.pdf"
Current Number string(1) "1"
string(5) "26:30"
newOrderNumber int(2)
filename to save as string(17) "mmOrderForm-2.pdf"
--
DB string(17) "mmOrderForm-2.pdf"
Current Number string(1) "2"
string(5) "26:31"
newOrderNumber int(3)
filename to save as string(17) "mmOrderForm-3.pdf"
--
DB string(17) "mmOrderForm-2.pdf"
Current Number string(1) "2"
string(5) "26:31"
newOrderNumber int(3)
filename to save as string(17) "mmOrderForm-3.pdf" <-- first occurrence
--
DB string(17) "mmOrderForm-3.pdf"
Current Number string(1) "3"
string(5) "26:32"
newOrderNumber int(4)
filename to save as string(17) "mmOrderForm-4.pdf"
--
DB string(17) "mmOrderForm-4.pdf"
Current Number string(1) "4"
string(5) "27:36"
newOrderNumber int(5)
filename to save as string(17) "mmOrderForm-5.pdf"
--
DB string(17) "mmOrderForm-5.pdf"
Current Number string(1) "5"
string(5) "27:37"
newOrderNumber int(6)
filename to save as string(17) "mmOrderForm-6.pdf"
--
DB string(17) "mmOrderForm-6.pdf"
Current Number string(1) "6"
string(5) "27:38"
newOrderNumber int(7)
filename to save as string(17) "mmOrderForm-7.pdf"
--
DB string(17) "mmOrderForm-7.pdf"
Current Number string(1) "7"
string(5) "27:39"
newOrderNumber int(8)
filename to save as string(17) "mmOrderForm-8.pdf"
--
DB string(17) "mmOrderForm-7.pdf"
Current Number string(1) "7"
string(5) "27:39"
newOrderNumber int(8)
filename to save as string(17) "mmOrderForm-8.pdf" <-- second occurrence
TL;DR: This didn't seem to fit the bill of any other questions here. Feels like a really random thing or something so incredibly obvious that I just don't see it. (probably the worst tr;dr ever, sorry)
sincerely... stumped.