Jump to content

cakephp newbie question on saving data


kee2ka4

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/199700-cakephp-newbie-question-on-saving-data/
Share on other sites

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.

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.