2

I am facing strange problem first time. I am trying to insert simple records in database with Custom Auto Increment Values e.g 00001, 00002, 00003. but unable to get Incremented value. Each record get updated with same max number.

Controller Code

public function dobulk() {
    for($i=0;$i<5;$i++) {
        $data = array();
        $this->Tmp->create();
        $data['Tmp']['invoice_no'] = $this->Tmp->get_no();
        $data['Tmp']['invoice_date'] = '2013-12-11';
        $this->Tmp->save($data);
    }
   }

Model Code

public function get_no() {
    $strSql = "SELECT
                LPAD(IFNULL(RIGHT(MAX(invoice_no),5),0) + 1,5,'0') AS max_id
        FROM tmps
        ;";

    $result = $this->query($strSql);
    $invoice_no = $result[0][0]['max_id'];
    return $invoice_no;
}

Database Table

CREATE TABLE `tmps` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `invoice_no` varchar(20) DEFAULT NULL,
  `invoice_date` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1

Thanks in advance.

1
  • 1
    Seems like this should be tagged as MySQL instead of CakePHP. You'll likely get more help that way, since it's a MySQL question. Commented Dec 12, 2013 at 5:45

2 Answers 2

1

You're probably running into the model cache.

From here: Bakery article on caching

If Model->cacheQueries == true, Cake will store the result of queries in memory. If it later sees an identical query, the result will be pulled from memory instead of hitting the database. This is only cached for the duration of a single page request. However, if a record is updated, the cache is not cleared. This is what gets most people unfamiliar with the cache.

So to make your code work, add this line to the top of the dobulk() function:

$this->Tmp->cacheQueries = false; 

As you're running raw SQL inside your model, you might also have to change the query request inside get_no() to:

$result = $this->query($strSql, false);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ben. Its really worked. I don't know how I missed Cache thing.
0

Hope below solution will help you!

   public function get_no() {
        $strSql = "SELECT MAX(invoice_no) AS max_id FROM tmps;";
        $result = $this->query($strSql);
        $invoice_no = $result['Tmp']['max_id'];
        return $invoice_no;
    }


    public function dobulk(){
      $data = array();
      $tempInvoiceNumber = $this->Tmp->get_no();
      for($i=1;$i<=5;$i++) {
        $data[$i]['Tmp']['invoice_no'] = tempInvoiceNumber+$i;
        $data[$i]['Tmp']['invoice_date'] = '2013-12-11';
       }
      $this->Tmp->saveAll($data);
    }

1 Comment

Thanks Anubhav, I will implement your solution, still I wonder what is root cause of this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.