0

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.

orderofout
  • 13
  • 2
  • The `orders_manufOrderForms.orderFormFilename` column has a unique index on it, right? Or, put another way, have you made sure the data in the table is actually correct and doesn't contain duplicates? – Charles Dec 10 '12 at 04:23
  • Regardless of the filename being unique or not, the fact is it shouldn't create a duplicate iteration because it's pulling the previous value and incrementing it. I've even set it to pull the id field that is generated via RedBean and increment that. This value is in fact unique. However, the result is the same. The counter value seems to stall at some point then resume again. – orderofout Dec 10 '12 at 04:33
  • I managed to find a work around, and perhaps a better approach to executing this script - I'm not considering it an answer because it doesn't explain the reason for the duplicates. Instead of using the `R::getAll()` function and striping the number I used the `R::count()` function to count the number of entries in the db then increment that number for the next one. Works like a charm, but provides no insight to answering the question. – orderofout Dec 10 '12 at 18:22

0 Answers0