[Dbix-class] inserting more records

Matt S Trout dbix-class at trout.me.uk
Thu Jan 11 22:32:14 GMT 2007


On 10 Jan 2007, at 14:50, Octavian Rasnita wrote:

> Hi,
>
> Is it possible to insert more records using the same code and just  
> adding a certain different field for each record?
>
> I wanted to do something like:
>
> my $rs = $schema->resultset("Table")->new({
> # here some more fields defined
> });

that's an object, not a resultset. REMEMBER THE DIFFERENCE BETWEEN  
RESULTSET OBJECTS AND RECORD OBJECTS!

DO NOT CONFUSE THEM LEST YE BE KILLED BY A GRUE, DEVOURED BY RABID  
ANTEATERS AND YOUR FIGGIN PLACED UPON A SPIKE.

> foreach my $user(@users) {
> $rs->user($user);
> }
>
> $rs->insert;
>
> This code inserts just the first record corresponding to the first  
> element of @users.

Well, given you only created one object and made one call to insert,  
DUH.

DBIx::Class cannot magically tell you want to do something other than  
you told it. It is software. If you need something that interprets  
half-complete requests and turns them into something sensible, you'll  
find it's called a consultant :)

Try

my @user_object = map { $rs->create({ %base_fields, user => $_ }) }  
@users;

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