[Catalyst] CRUD Question. How to update a record.

Hans Ophüls hop at shoppilot.de
Mon Feb 19 22:41:07 GMT 2007


> But I have a problem with implementing an
> update function.
> It schould select a record from the database
> and insert the data into a widget.
> 
> sub hw_update : Local {
>     my ($self, $c, $id) = @_;
> 
>     # Search for the product 
>     c->model('MyAppDB::Product')->search({id => $id});
> 
>     # Create the widget and set the action for the form
>     my $w = $self->make_product_widget($c);
>     $w->action($c->uri_for('hw_create_do'));
> 
> ....
> 
> sub make_product_widget {
>     my ($self, $c) = @_;
> 
>     # Create an HTML::Widget to build the form
>     my $w = $c->widget('product_form')->method('post');
> 
>     # Create the form fields
>     my $e = $w->element('Textfield', 'itemid'  
> )->label('ItemId')->size(30);
>     $e->value('4711');
> 
> Just where there is '4711' should be the value from the 
> column 'itemid'.
> I have tried various expression with
> $c->model('MyAppDB::Product')...
> but I have not found the right way.

I have found a way to do it.

sub hw_update : Local {
    my ($self, $c, $id) = @_;
    $c->stash->{product} =  $c->model('MyAppDB::Product')->search({id => $id});
    # Create the widget and set the action for the form
    my $w = $self->make_product_widget($c);
    $w->action($c->uri_for('hw_create_do'));

....

sub make_product_widget {
    my ($self, $c) = @_;

    # Create an HTML::Widget to build the form
    my $w = $c->widget('product_form')->method('post');

    if (defined $c->stash->{product}) { # update

        my $firstrecord = $c->stash->{product}->next;

        # Create the form fields
        my $e = $w->element('Textfield', 'itemid'  )->label('ItemId')->size(30);
        $e->value($firstrecord->itemid);
   ...
   } else { # create
   ...

I don't now if this is best way to do it.
But I have now a widget, which can be used for update and create operation.

But something goes wrong with Umlaut i.e. üäö .
They are stored in the database as Unicode. 
Also all Pages are deliverd with utf-8 charset.

But in the update form those characters are not displayed
correctly (In the product list they are displayed correctly).
It seems to me that something goes wrong with my

        $e->value($firstrecord->itemid);

Any ideas ?

Hans




More information about the Catalyst mailing list