[Dbix-class] question about DBIx::Class::HTMLWidget
John Napiorkowski
jjn1056 at yahoo.com
Fri Jul 14 08:43:16 CEST 2006
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/ +
More information about the Dbix-class
mailing list