On 21 Feb 2006, John Siracusa wrote:
Thanks for your answer and sorry for my late reply, I couldn't work
on my project for a few days.
> 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.
I want them appropriate for displaying in the web form. This seems to
work only if I leave out the (un)set_state_saving thing.
> If you *do* want to
> custom-format, say, DateTime values, there's a better approach than what
> you're doing here:
> [...]
> Try something like this instead:
>
> if(UNIVERSAL::isa($val, 'DateTime'))
> {
> $hash{$field} = $val->sprintf('%m.%d.%Y');
> }
Thanks, this looks better and it works if I change it to:
$val->strftime('%d.%m.%Y');
So, this is the resulting method (in my base class):
sub as_hashref {
my $self = shift;
my %hash;
# set_state_saving($self); # doesn't work (wrong date format)
foreach my $column ($self->meta->columns) {
my $method = $column->accessor_method_name;
if(UNIVERSAL::isa($self->$method, 'DateTime')) {
$hash{$column} = $self->$method->strftime('%d.%m.%Y');
} else {
$hash{$column} = $self->$method;
}
}
# unset_state_saving($self);
return \%hash;
}
> > 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.
I didn't find something as clean as the above method yet.
> > 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.
Well, I got Rose::HTML::Object but on a first glance it doesn't look
like I could easily integrate it. I use Template Toolkit, CGI.pm,
HTML::FormValidator for layout control, form variables and Validation
respectively.
If I see it right I had to replace all this well known stuff to the
equivalents in Rose::HTML::Objects, without even knowing if there is
an equivalent for everything. I don't think I want to go that far at
the moment, especially since I don't think it a good idea to abondon
template based HTML generation.
But perhaps my first look at it was too superficial?
> (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 :)
I waited for the new release but I must admit I am still not totally
convinced.
Given the widespread use of CGI.pm I would find it of great help if I
could stick to a CGI query object and don't have to use a
Rose::HTML::Object.
I know these should be methods of CGI.pm but I would appreciate
something like object_from_form and init_with_object in RDBO with
object = <CGI.pm query object>.
Anyway, thanks for your help, it really gave me some important
insights.
Michael
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object