Thanks for your answer.
I think that I do need separate tables or something like it. For example, I would like to be able to have InvoiceStatus 20 and OrderStatus 20, but within the same context (e.g. OrderStatus) it should only be possible to have one record with number 20.
When I force the table to have a unique index on the number column, I would not be able to use 20 as a number on both Invoice and Order. That's why I would like to have 2 tables, to be sure a number cannot be used twice within the entity it is related to. Probable I should put an index on number and the classname column, but is that possible?
Another option would be to define the status simply as an Enum, but that does not give me enough information. Enum is just Enum.
And a little pity is that I cannot use SQL (I think) to fetch e.g. all Orders with status 20 or up, can I?
FYI: The full code I was using (only one subclass as an example)
class Code extends DataObject {
static $db = array(
'Number' => 'Int',
'Description' => 'Varchar(100)',
'Notes' => 'Text',
);
static $indexes = array(
array( 'type' => 'unique', 'value' => 'Number' )
);
public function getCMSValidator() {
return new Code_Validator();
}
}
class Code_Validator extends RequiredFields {
protected $customRequired = array('Number','Description');
protected $entity = "Code";
/**
* Constructor
*/
public function __construct() {
$required = func_get_args();
if(isset($required[0]) && is_array($required[0])) {
$required = $required[0];
}
$required = array_merge($required, $this->customRequired);
parent::__construct($required);
}
function php($data) {
$valid = parent::php($data);
$number_SQL = Convert::raw2sql($data['Number']);
$codes = DataObject::get($this->entity, "`Number` = '{$number_SQL}'");
if($codes !== null && ((int)$codes->First()->ID != (int)$data['ID'])){
$this->validationError("Number", sprintf( "Sorry, a record with number %d already exists.", $data['Number']));
$valid = false;
}
return false;
}
}
and OrderStatus
<?php
class OrderStatus extends Code {
public function getCMSValidator() {
return new OrderStatus_Validator();
}
}
class OrderStatus_Validator extends Code_Validator {
protected $entity = "OrderStatus";
}