[Dbix-class] inserting more records

Jess Robinson castaway at desert-island.demon.co.uk
Wed Jan 10 18:10:44 GMT 2007



On Wed, 10 Jan 2007, 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
> });

This produces a Table object, not a resultset, see the 
DBIx::Class::ResultSet docs:

        IMPORTANT: If called on an object, proxies to new_result instead so

          my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });

        will return a CD object, not a ResultSet.


> foreach my $user(@users) {
> $rs->user($user);
> }
>
> $rs->insert;

If all you are doing is creating new rows, and throwing away the objects, 
then you could just use populate() instead:

$schema->populate('Table', [
                   [ 'user' ],          # list of field names
                   ['username1'],       # values for the fields
                   ['username2'],       # values for another row
                  ]);

> This code inserts just the first record corresponding to the first element of 
> @users.
>
> Do I need to create a new $rs for each element of @users?

No, you could also just empty the PK cols and unset in_storage(), but 
populate is the correct way to do this.


Jess




More information about the Dbix-class mailing list