On 2/21/06 3:00 PM, Michael Lackhoff wrote:
> My new project is a web based application with many forms that
> represent more or less a database table each and I have to write many
> forms to edit or add records.
> I use CGI-Application with a Plugin that makes it very easy to fill a
> form. This plugin usually takes two parameters: the HTML of a form
> (usually from a template) and a hashref with field names as keys.

Ah, okay.  For the record, I use an object-based approach to this kind of
thing by using Rose::HTML::Objects to manage my forms.  Anyway, getting a
hashref from an RDBO is pretty straight-forward.  The only tricky bit is
convincing the object accessors to deflate any "rich" values (e.g., DateTime
objects).  Here's an example of that:

    # $o is a Rose::DB::Object-derived object

    # Import utility functions used to convince $o to deflate values
    # as if it's saving to the database, and to restore the state later.
    use Rose::DB::Object::Util qw(set_state_saving unset_state_saving);

    my %hash;

    set_state_saving($o);

    foreach my $column ($o->meta->columns)
    {
      my $method = $column->accessor_method_name;
      $hash{$column} = $o->$method();
    }

    unset_state_saving($o);

What you'll get in the hash values are strings appropriate for storage in
the database, which may or may not be what you want.  If you *do* want to
custom-format, say, DateTime values, there's a better approach than what
you're doing here:

>     foreach my $field (@columns) {
>         if (ref $rose->$field eq 'DateTime') {
>             my $date = $rose->$field;
>             $date =~ s/^(\d{4})-(\d{2})-(\d{2}).*/$3.$2.$1/;
>             $hash{$field} = $date;

Try something like this instead:

    if(UNIVERSAL::isa($val, 'DateTime'))
    {
      $hash{$field} = $val->sprintf('%m.%d.%Y');
    }

This is why we use "rich" objects, after all :)  Check the DateTime
documentation for a full list of methods.

> It would be very handy if I could just say
> my $hashref = $obj->as_hash_ref;
> instead.

As I hope you can see above, there are many decisions to be made about
exactly what the keys and values of that hash might be.  So it's not exactly
as straightforward as you might think to add an API like that to the core
distribution.  Thankfully, it's easy to add such a method to your common
base class that does what *you* want it to do.

> The same problem arises when the data comes back from the form. I
> have a CGI query object and CGI.pm has the Vars-function that gives
> me all the form data in a hashref. It would be very helpful if I
> could pass the result of $q->Vars or perhaps the whole object to my
> Rose-object, do a 'save' and everything is fine (again I would have
> to change the date fields to a format that is allowed, so again it
> would be helpful if I could define my default date format).

Again, I use Rose::HTML::Form for this kind of stuff.  But if you want to
use hashrefs and so on, you'd basically just do the reverse of the examples
shown above.  And likewise, once you decide what to do and how to do it,
it's easy to add the appropriate method(s) to your common base class,
letting all your RDBO-derived objects reap the benefits.

> Here is the code I use at the moment to achieve this:
> ...
>     foreach my $field (@columns) {
>         next unless exists $daten{$field};
>         if ($daten{$field} eq '') {
>             $daten{$field} = undef;
>         } elsif ($daten{$field} =~ /(\d{1,2})\.(\d{1,2})\.(\d{4})/) {
>             $daten{$field} = sprintf("$3-%02d-%02d", $2, $1);
>         } 
>         # $rose->{$field} = $daten{$field};
>         $rose->$field($daten{$field});
>     }
>     return $rose;
> }
> 
> It works but looks quite clumsy, so I wanted to ask if there is a
> more elegant shortcut to achieve this common task.

Ick :)  Consider Rose::HTML::Objects.  In particular, take a look at the
object_from_form() and init_with_object() methods of Rose::HTML::Form.

(Also note that a major Rose::HTML::Object update is due out any day now (as
soon as I finish the docs...) which will greatly simplify the process of
setting up a form.  So please don't be too scared away by the slightly
verbose nature of the examples in the existing docs :)

-John




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to