This project is read-only.

How to create a content type with sequential identities

Topics: Core, General
Dec 23, 2012 at 12:35 AM

I have a content type for product orders "Order" and ideally I would like the identity field to be sequential, or to at least have a sequential "OrderNumber" field.

Currently content items of all content types in Orchard share the same identifier in the Orchard_Framework_ContentItemRecord table.

So if I have a new customer to the site and they create a new order that contains two order lines, I get a customer with id 150, the order has id 151 and the order lines get 152 & 153. Then the next order gets order id 154.

I'm thinking it would be good to have some kind of "Sequential Identity" part that could be added to a content type that would give you a "Number" field that would automatically get the next value in the sequence for that content type when the content type is being created. The part could have some options to allow you to set the initial (or next) id value for the content type it's associated with.

So I guess the question is, does any capability like this already exist in Orchard?

If not, is this something that could be integrated into the core "Identity" part? So if you're content type has the Identity part, in the content type definition, some options could exist when you expand out the Identity part (eg. "Use sequential id for this content type" and "Next id to use"). If this option was enabled, the content item's true identity (in the Orchard_Framework_ContentItemRecord table) would not be exposed, instead a content type specific id could be exposed. This could even go so far as being used in the URL routing for the content type. 

Otherwise I'm thinking a "Sequential Identity" part might be the way to go?

Any thoughts would be appreciated.

Dec 23, 2012 at 6:12 AM

Why? Sequential numbers basically don't work if you ever have more than one user of the system. Sequences and concurrency basically are a non-starter.

Dec 23, 2012 at 6:33 AM

It's really just to provide a nicety to the merchant. Instead of having huge gaps in the order numbers (potentially 10's or 100's if other unrelated content items are being created frequently) I'd like to keep the order number as sequential as possible to give an indication of the number of orders in the system.

Surely there could be other use cases in the real world for a sequential number field (doesn't need to be id) for a content type?

I understand there might be issues around concurrency and ensuring sequential numbering. I still expect there to be gaps (eg. an order is deleted or two users create orders at the same time and one cancels the order or some such thing). I also understand there's potential for performance bottleneck here, but in this case I'm not expecting a lot of concurrent ordering.

In terms of managing the concurrency issue, could the feature use DB level transaction isolation to achieve a unique number? Eg. With SQL Server you can use the OUTPUT clause (not sure about MySQL or other DBMS)...

UPDATE SequenceNumber
SET NextValue = NextValue + 1
WHERE PartId = @partID

Just thinking out-loud. If it's all too much trouble (especially to support multiple DBMS) then I can live without sequential numbers :o)

Dec 23, 2012 at 8:41 AM

Fine. You can implement anything you want in your own modules, but this will never be part of core.