[development] Form field maxlengths vs. database field sizes

Larry Garfield larry at garfieldtech.com
Thu Feb 9 16:41:16 UTC 2006

On Thu, February 9, 2006 7:47 am, lodewijk evers said:
>> > This issue stems from Killes' comment [
>> > http://drupal.org/node/46306#comment-71971 ]. Should form field
>> > maxlengths be consistent with corresponding database field sizes or
>> > should unicode encoding be accounted for?
>> Drupal has no way of knowing what the field size of the
>> database is, and as such the forms api can't take that into account.
> Achtually, php can get the complete properties of a mysql
> database/table/field ..including maximum field sizes ( mysql_field_len(),
> mysql_field_type() and mysql_field_flags() ),
> For postgres if's similar ( pg_field_size() ).
> So it must be possible for drupal to get to that information too.
> Regards,
> Lodewijk Evers
> http://www.ontwerpwerk.nl

Allowing Drupal to auto-derive database information would be extremely
useful.  For instance, since many many nodes have a 1:1 mapping from their
database table to their Drupal object, if the system "knows" what those
fields are it can automate the basic hook_load(), hook_save(), and
hook_insert() calls.  That means less redundant code.  (We could actually
do that now without db introspection if a node could be asked for an array
of its fields, but I've avoided trying to add that until after 4.7 ships.

A step further, as I'm sure others are thinking, would be for Drupal to
read the database table and pre-populate a $form object.  It wouldn't be
perfect, but it would again reduce the amount of code needed in each node
module.  Lots 'o fun.

However, I would say up front do NOT derive that information on the fly! 
If every node_load() call has to rederive the structure of the node from
the database, that's a big performance hit.  That's the sort of
information that can/should be build once when the module is enabled and
then cached.  PEAR and RoR do it through code generation, but to be honest
I've never been a fan of that.  If the information is standardized enough
that it can be auto-generated, then it is standardized enough that it can
be fed as input values into a common routine/function.

--Larry Garfield

More information about the development mailing list