Jump to content

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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.