Is there a recommended way to share a single serial field between two node types?

Would changing serial.inc:

function _serial_get_table_name($field) {
  return db_escape_table( // be on the safe side
    'serial_' . $field['type_name'] . '_' . $field['field_name']);
}

to

function _serial_get_table_name($field) {
  return db_escape_table( // be on the safe side
    'serial_' . $field['field_name']);
}

suffice? albeit make all serial fields shared which is OK for this application?

any advice appreciated, thanks

DT

Comments

kirsh’s picture

Assigned: Unassigned » kirsh

Sharing the table by several types also requires changing the activity when a new field is added (currently the table is created) and removed (the table is dropped and the remaining fields will not have any table). I think that that is but there might be other issues.

davidwhthomas’s picture

Status: Active » Postponed

Thanks for the reply, yes that would be the case. I was looking into checking if the table exists prior to creation and checking if it's used by other types prior to delete.

In any case, for the time being I've ended up using a postgres sequence with a cck computed field as I'd rather not hack on the serial module which is great for per type serial values.

<?php
// in an update_n hook
$ret = update_sql("CREATE SEQUENCE my_serial_seq START 10000000");
?>

and for the computed field:

<?php
return db_result( db_query("SELECT nextval('my_serial_seq')") );
?>

Possibly in the future shared field support could be added however.

Nevertheless, cool module, thanks for the help.

DT

colan’s picture

Title: support for a shared serial field? » Share serial fields
Version: 6.x-1.0-beta2 » 7.x-1.x-dev

No new features for D6.

ikeigenwijs’s picture

Issue summary: View changes
Status: Postponed » Active

When a serial field is reused on a different contenttype it would be logical that the serial field counts upwards.
Use case:
1 incremental numer shared over multiple contenttypes but not all.

Its still posible to use a per contenttype serial field.
But it would also be posible to share a serial field.

example:
contenttype Article:

  • Serial_article_1
  • Serial_shared

contenttype Page:

  • Serial_page_1
  • Serial_shared

Adding new contenttype
add new Article: Serial_article_1 (1) ,Serial_shared (1)
add new Article: Serial_article_1 (2) ,Serial_shared (2)
add new Page: Serial_page_1 (1) ,Serial_shared (3)
add new Page: Serial_page_1 (2) ,Serial_shared (4)
add new Article: Serial_article_1 (3) ,Serial_shared (5)
s

MustangGB’s picture

Possible quick and dirty fix, not fully tested.

serial.inc

 function _serial_get_table_name($bundle, $field_name) {
-  return db_escape_table('serial_' . $bundle . '_' . $field_name);
+  return db_escape_table('serial_' . $field_name);
 }
MustangGB’s picture

Follow-up comment to say I've been using #5 for the last 3 months, it works as expected and I haven't ran into any issues.

Eric_A’s picture

I don't have the time right now, but I think this could/should be implemented as a field setting with full BC.

Here is a snippet that is #5 re-rolled against 7.x-1.8 and with some added comments. Of course this type of change is a use-at-your-own-risk-if-you-think-you-know-what-you-are-doing.

diff --git a/serial.inc b/serial.inc
index 52ed5d1..b2d8b21 100644
--- a/serial.inc
+++ b/serial.inc
@@ -101,6 +101,10 @@ function _serial_get_field_table_name(array $field, array $instance) {
 function _serial_get_table_name($entity_type, $bundle, $field_name) {
   // Remember about max length of MySQL tables - 64 symbols.
   // @todo Think about improvement for this.
+  // @todo Convert to field setting with BC. Clean up on field delete.
+  if (TRUE) {
+    return db_escape_table('serial_' . md5("{$entity_type}_{$field_name}"));
+  }
   return db_escape_table('serial_' . md5("{$entity_type}_{$bundle}_{$field_name}"));
 }