[Dbix-class] update_or_insert from hash reference
Fernan Aguero
fernan at iib.unsam.edu.ar
Tue May 23 18:30:48 CEST 2006
Hi!
I'm new to DBIx::Class and after setting up a mysql database
I'm trying to test DBIx::Class by defining a minimal
object-relational mapping for a single table:
TABLE users (
user_id (PK, auto_increment),
username,
firstname,
lastname.
is_allowed_to_edit_database,
date_modified,
date_added )
What I want to do to populate the DB is something like this:
my $userHashRef = {
username => $username,
firstname => $firstname,
lastname => $lastname,
... ...
};
And then
# get an object
my $appUser = MyApp::DB::User->new( $userHashRef );
# update or insert
$appUser->update_or_insert;
However, I don't know if what I'm trying to do is possible
... reading the pod for DBIx::Class::Row suggests to me
that this can be done. So I went ahead and did this:
package MyApp::DB::Users;
use base qw/DBIx::Class/;
use DBIx::Class::Row;
__PACKAGE__->load_components( qw/Core/ );
# set the table name
__PACKAGE__->table('users');
# set columns in the table
__PACKAGE__->add_columns( qw/
user_id username password date_modified date_added firstname
lastname is_allowed_to_edit_database / );
# set the primary key
__PACKAGE__->set_primary_key( qw/ user_id /);
sub new {
my ( $class, $hashRef ) = @_;
$class = ref( $class ) || $class;
my $userObj = DBIx::Class::Row->new( $hashRef );
bless( $userObj, $class );
return $userObj;
}
and in my insertdb test script:
use MyApp::DB::Users;
my $userObj = MyApp::DB::Users->new( $userHashRef );
$userObj->update_or_insert;
However, I'm getting the following error:
Can't locate object method "has_column" via package "DBIx::Class::Row" at /usr/local/lib/perl5/site_perl/5.8.8/DBIx/Class/Row.pm line 42.
What am I missing? What am I doing wrong? Thanks in advance,
Fernan
PS: I know there are other ways to populate the DB. I've
read the DBIx::Class::Manual::Example but I found the
insertdb.pl example (using ->populate) to be more confusing
to read and understand than the above.
More information about the Dbix-class
mailing list