FAQ
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."

I get the feeling I need to do something with:

my $roles_rs = $c->model('db::membership::roles');

But all the stuff I tried, like ->search doesn't seem to help. Has anyone run into this before and found the solution? BTW I'm using the version from CPAN labled '0.06'.


--john

Search Discussions

  • Ash Berlin at Jul 13, 2006 at 2:45 pm

    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."

    I get the feeling I need to do something with:

    my $roles_rs = $c->model('db::membership::roles');

    But all the stuff I tried, like ->search doesn't seem to help. Has anyone run into this before and found the solution? BTW I'm using the version from CPAN labled '0.06'.


    --john


    _______________________________________________
    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/dbix-class at lists.rawmode.org/
    you need to have "load_components(qw/HTMLWidget Core/);" in the roles
    class.

    Ash
  • Matt S Trout at Jul 13, 2006 at 3:19 pm

    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/ +
  • John Napiorkowski at Jul 14, 2006 at 6:43 am
    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 <dbix-class at trout.me.uk>
    To: John Napiorkowski <jjn1056 at yahoo.com>; dbix-class at lists.rawmode.org
    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/ +

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbix-class @
categoriesperl, catalyst
postedJul 13, '06 at 2:29p
activeJul 14, '06 at 6:43a
posts4
users3
websitedbix-class.org
irc#dbix-class

People

Translate

site design / logo © 2021 Grokbase