[Catalyst] Insert Multiple rows

David Romano david.romano at gmail.com
Mon Dec 19 17:49:15 CET 2005


On 12/19/05, Will Smith <willbelair at yahoo.com> wrote:
> Could I use this (obviously not, because I got error): for example, I have 2
> boxes named artistid
> sub do_insert : Local {
>     my ( $self, $c ) = @_;
>     my $myvar;
>     for ($myvar=0;$myvar<2;$myvar++)
>     {
>     my $cd = mymusic::M::CDBI::Cd->find_or_create(artistid
> => $artist); # the error is here , I get only 1 row created instead of 2 as
> desired. When tried to use ->create only, it says need to get hash ....  so
> I changed to : my %hash = (artistid => $c->req->param('artistid')); and
> still get error
>     }
>
>     $c->stash->{template} = 'Cd/list.tt';
> }
See if changing:
>     for ($myvar=0;$myvar<2;$myvar++)
>     {
>     my $cd = mymusic::M::CDBI::Cd->find_or_create(artistid
> => $artist); # the error is here , I get only 1 row created instead of 2 as
> desired. When tried to use ->create only, it says need to get hash ....  so
> I changed to : my %hash = (artistid => $c->req->param('artistid')); and
> still get error
>     }
to:
for (@$array) {
    my $cd = mymusic::M::CDBI::Cd->find_or_create(artistid => $_);
}

I think the reason you're getting an error is that $artist is NOT a
scalar, but a reference to an array (you might want to change the name
of your variable, as shown by Johan, to reflect this), and so you
first need to dereference it (e.g., @$array) and then access each item
in the array. If you thing about your above loop, you're not even
using $myvar, or changing access to $artist, so it's probably trying
to insert the same value two times in a row.

HTH,
David



More information about the Catalyst mailing list