Turns out that my $new_rs = $old_rs->new_result({}) did the trick, as Matt 
suggested.  It does require a hash reference but you can just put an empty one 
in and it works fine (at least on my postgresql setup).  I inlined the updated 
code in case anyone is interested.  

This works really well and speeds up coding while making things look a lot 
neater.

--john

sub create :Path('create') Args(0)
{
    my ($self, $c) = @_;
    
        #####--> here's where the fix went <---#####
        my $roles_rs    = $c->model('db::membership::roles')->new_result({});
        
        my $form = $self->role_form($c);
        my $result    :Stashed;

        if( $c->request->method eq 'GET' )
        {            
            ## Get the result object
            $result    = $form->process();
        }
        elsif($c->request->method eq 'POST')
        {
            ## Get the result object
            $result = $form->process($c->request);
            
            unless ($result->has_errors)
            {
                ##Put DB Data
                $roles_rs->populate_from_widget($result);
            
                ##Return to the list
                $c->response->redirect($c->uri_for($c->action));
            }
        }
}

----- Original Message ----
From: Matt S Trout <[EMAIL PROTECTED]>
To: John Napiorkowski <[EMAIL PROTECTED]>; [email protected]
Sent: Thursday, July 13, 2006 11:19:23 PM
Subject: Re: [Dbix-class] question about DBIx::Class::HTMLWidget


On 13 Jul 2006, at 15:29, John Napiorkowski wrote:

> Hi,
>
> I hope it's cool to post questions about this module on the DBIx  
> Class list.  I'm having trouble figuring out how to use this to  
> create new entries in the database.  I was able to get it to edit  
> existing ones without any trouble.  I can see in the source code  
> that it's using 'insert_or_update' so I imagine that the intention  
> at least is for this toe be able to create new entries.
>
> Here's my source (I'm using Catalyst):
>
> sub create :Path('create') Args(0)
> {
>     my ($self, $c) = @_;
>
>         my $roles_rs  = $c->model('db::membership::roles');
>
>         my $form = $self->role_form($c);
>         my $result    :Stashed;
>
>         if( $c->request->method eq 'GET' )
>         {
>             ## Get the result object
>             $result    = $form->process();
>         }
>         elsif($c->request->method eq 'POST')
>         {
>             ## Get the result object
>             $result = $form->process($c->request);
>
>             unless ($result->has_errors)
>             {
>                 ##Put DB Data
>                 $roles_rs->populate_from_widget($result);
>
>                 ##Return to the list
>                 $c->response->redirect('/roles');
>             }
>         }
> }
>
> And the error I get is from Catalyst is:
>
> Caught exception in talentspace_portal2::Controller::roles->create  
> "Can't locate object method "populate_from_widget" via package  
> "talentspace::Schema::db::membership::roles::_resultset" at /mnt/ 
> hgfs/talentspace/talentspace_portal2/script/../lib/ 
> talentspace_portal2/Controller/roles.pm line 108."

Err, well, yes. populate_from_widget is an object method, not a  
resultset method. Create a new object via $rs->new_result and then  
call populate_from_widget on *that* - the insert_or_update call will  
ensure it does the right thing.

-- 
Matt S Trout, Technical Director, Shadowcat Systems Ltd.
Offering custom development, consultancy and support contracts for  
Catalyst,
DBIx::Class and BAST. Contact mst (at) shadowcatsystems.co.uk for  
details.
+ Help us build a better perl ORM: http://dbix- 
class.shadowcatsystems.co.uk/ +






_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/[email protected]/

Reply via email to