Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Invoice Numbering


Go to End


2 Posts   770 Views

Avatar
peterpall18

Community Member, 1 Post

11 April 2013 at 3:59am

Edited: 11/04/2013 4:00am

Hi

I'm trying to populate the invoiceID by adding +1 to the previous number but it won't work. No error message, just a blank page.

Here's my code:

class Invoice extends DataObject {

static $db = array(
'InvoiceID' => 'Int'
);

public function populateDefaults() {

parent::populateDefaults();

$obj = Invoice::get()->sort('Created', 'ASC')->First();

$this->InvoiceID = $obj->InvoiceID+1;
}

}

Why isn't working?

Avatar
Willr

Forum Moderator, 5523 Posts

11 April 2013 at 7:59pm

What happens if there is no invoices in the system? (you should use $this->InvoiceID = ($obj) ? $obj->InvoiceID+1: 1);

Also this way you've implemented will not check for duplicates of anything like that. You could just just the automatic primary $ID key that is defined on every DataObject. Check your php error logs for any other syntax issues that could be causing an issue.