[Catalyst] Create multiple records

Jonathan Rockway jon at jrock.us
Fri Dec 29 20:07:45 GMT 2006


Will Smith wrote:
> Hi,
> Could someone please show me the syntax to create multiple records. For
> example my table book has 3 fields : id (auto_increment), title, price.
> On the template I have 3 rows of title/price. I want to enter 3 book
> titles and prices and create at the same time.
> With the old syntax, I can do something like
> my $book = myapp::M::CDBI::Book::Shift->create(\%hash);

Are you really using CDBI?  If so, switch to DBIC and do:


   push @records, {title => "foo", price => "13.37"};
   # same thing a few more times...
   eval {
      $c->model('BookDB::Books')->create($_) for (@records);
   }
   die "Error: $@" if($@);

or something similar.

-- 
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)->config(name => do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
";$;"]->[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;->setup;



More information about the Catalyst mailing list