Author Topic: cakephp newbie question on saving data  (Read 612 times)

0 Members and 1 Guest are viewing this topic.

Offline kee2ka4Topic starter

  • Enthusiast
  • Posts: 68
    • View Profile
cakephp newbie question on saving data
« on: April 25, 2010, 02:15:26 PM »
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?

Offline trevorsg

  • Irregular
  • Posts: 7
    • View Profile
Re: cakephp newbie question on saving data
« Reply #1 on: April 25, 2010, 11:18:40 PM »
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.

Offline 244863

  • Irregular
  • Posts: 29
    • View Profile
Re: cakephp newbie question on saving data
« Reply #2 on: May 04, 2010, 08:07:54 AM »
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.