Wednesday, December 11, 2013

Saving Multiple Records in look

Hello,

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.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: