DBIx-DataModel

 view release on metacpan or  search on metacpan

lib/DBIx/DataModel/Doc/Quickstart.pod  view on Meta::CPAN


The other form of C<insert()> is to supply one or several hashrefs, where each
hashref corresponds to a record to create : 

  my ($bach_id, $berlioz_id, $monteverdi_id)
    = HR->table('Employee')->insert(
      {firstname => "Johann",  lastname => "Bach"      },
      {firstname => "Hector",  lastname => "Berlioz"   },
      {firstname => "Claudio", lastname => "Monteverdi"},
    );

The result is the same in both cases.

In this example, it is assumed that keys are generated automatically
within the database (see the C<AUTO_INCREMENT> clause in the
L</"BASIC ASSUMPTIONS"> section); therefore  they need not be supplied here.
The return value of the method is the list of ids generated by the database.

Next we create some departments and skills,
here with explicit primary keys, and using both insertion syntaxes :

  HR->table('Department')->insert(
    {dpt_code => "CPT",  dpt_name => "Counterpoint" },
    {dpt_code => "ORCH", dpt_name => "Orchestration"},
  );

  HR->table('Skill')->insert(
    [qw/ skill_code  skill_name /],
    [qw/ VL          Violin     /],
    [qw/ KB          Keyboard   /],
    [qw/ GT          Guitar     /],
   );

=head3 C<insert_into_*()> methods

For inserting data into the C<Activity> table, instead
of addressing the table directly, we can
take advantage of the C<insert_into_activities>  in the
associated C<Employee> class : 

  my $bach = HR->table('Employee')->fetch($bach_id); 
  
  $bach->insert_into_activities({d_begin => '01.01.1695',
			         d_end   => '18.07.1750',
			         dpt_code => 'CPT'});

In addition to the columns explicitly listed above, this method
automatically adds the foreign key C<< emp_id => $bach_id >> that
will link the activity to the C<$bach> employee. The same can 
be done for employee skills :

  $bach->insert_into_emp_skills({skill_code => 'VL'},
			        {skill_code => 'KB'});


=head3 Cascaded inserts

Since there is a
L<composition|DBIx::DataModel::Doc::Glossary/"composition">
between classes C<Employee> and C<Activity>, we can supply a
whole data tree to the C<insert()> method, and cascaded inserts
will be performed automatically :

  HR->table('Employee')->insert(
    {firstname  => "Richard",  
     lastname   => "Strauss",
     activities => [ {d_begin  => '01.01.1874',
                      d_end    => '08.09.1949',
                      dpt_code => 'ORCH'      } ]}
  );

For retrieving the keys of records generated by those insertions, we
can use the C<< -returning => {} >> option :

  my $data = {firstname  => "Richard",  
              lastname   => "Strauss",
              activities => [ {d_begin  => '01.01.1874',
                               d_end    => '08.09.1949',
                               dpt_code => 'ORCH'      } ]};
  my $ids = HR->table('Employee')->insert($data, -returning => {});
  # ids now contains : { emp_id     => ..., 
  #                      activities => [{act_id => ...}]};


=head2 Updating data

The C<update()> method can be used either as a class
method, like this :

  HR->table('Employee')->update($bach_id => {firstname => "Johann Sebastian"});

or as an instance method, like this :

   my $bach = HR->table('Employee')->fetch($bach_id);
   $bach->update({firstname => "Johann Sebastian"});

Using named parameters, the class method can also update several
records in one single instruction :

  HR->table('Employee')->update(
    -set   => {retired => 'true'     },
    -where => {age     => {">" => 65}},
    );


=head2 Deleting data

The C<deleting()> method can be used either as a class
method, like this :

  HR->table('Employee')->delete($bach_id);

or as an instance method, like this :

   my $bach = HR->table('Employee')->fetch($bach_id);
   $bach->delete;

Using named parameters, the class method can also delete several
records in one single instruction :

  HR->table('Employee')->delete(-where => { age => {">" => 65} }



( run in 0.922 second using v1.01-cache-2.11-cpan-39bf76dae61 )