kee2ka4 Posted April 25, 2010 Share Posted April 25, 2010 Hey guys, I am a newbie in cakePHP and need some help. I have the following code in the add() function of the invoices_controller: function add() { if (!empty($this->data)) { $this->Invoice->create(); if ($this->Invoice->save($this->data)) { $this->Session->setFlash(__('The Invoice has been saved', true), 'success'); $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The Invoice could not be saved. Please, try again.', true)); } } } The data is collected from the form in the add view, but I also have a field called "TransactionNum" which needs to store a random generated Number along with the id generated after adding a new record in the database. So say the add(), successfully added the following record: invoice.id = 12, I want to immediately update the TransactionNum with "random number + id" to give it a unique transaction number. Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/199700-cakephp-newbie-question-on-saving-data/ Share on other sites More sharing options...
trevorsg Posted April 26, 2010 Share Posted April 26, 2010 Random number + id won't produce a unique ID (in other words, it's just as "random" as a regular random number). You might want to use http://us2.php.net/manual/en/function.uniqid.php to generate a unique ID. At any rate, you can access the ID of the new record that was saved with $this->Invoice->id. If you want to run two queries on every save that's your choice. Quote Link to comment https://forums.phpfreaks.com/topic/199700-cakephp-newbie-question-on-saving-data/#findComment-1048320 Share on other sites More sharing options...
244863 Posted May 4, 2010 Share Posted May 4, 2010 Just create a field in your table called id char(36) and cake will auto place a random id in there without you doing anything. Quote Link to comment https://forums.phpfreaks.com/topic/199700-cakephp-newbie-question-on-saving-data/#findComment-1052976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.