From: "Jeff Pang" <pa...@arcor.de>
I saw many companies require the employees to use DBIx::Class for work.
I know this is a ORM system for database with Perl.
What's the advantage of using ORM?

Regards.



With an ORM you can do the same things as with DBI directly (because the Perl ORMS finally use DBI to access the database), but you can do it more simple.

An ORM is a higher-level module than DBI that helps you connect to the database, insert data, get data, change data, but without using SQL.

The code needed to use an ORM is much elegant than SQL so it is far more easier to maintain because you need to use just methods and Perl data structures (hashes and arrays) and you don't need to concatenate SQL strings in order to construct complex SQL queries depending on the user input.

The result from an ORM is not a data structure like DBI offers, but usually an object that has more methods, and those methods could return not only the raw data from the database, but it can return it as an object. For example, such an object instead of beeing a date like '2011-01-15', it could be a DateTime object that holds that date, so you can get easier the current month, or the week day, or the localized month name and so on. And of course, when you want to insert the data in the database you could also give it a DateTime object...
And DBIx::Class can use other inflators and deflators than DateTime.

Another big advantage is the fact that DBIx::Class will understand the relation between tables and it can get the relevant data in a more simple way.

Here is a short example of using DBIx::Class:

#Connect to the schema for the database that you generated (with a command line):
my $schema = My::DB->connect("dbi:mysql:database=test", "user", "pass");

#Insert some data in the user table:
$schema->resultset("Users")->create({
 first_name => $first_name,
 last_name => $last_name,
 password => $password,
 email => $email,
});

#Select the user that has the first name "John" and the last name starts with "Smi":
my $user = $schema->resultset("Users")->search({
 first_name => "John",
 last_name => {-like => "Smi%"},
});

#Print the email address of this user:
print $user->email;

#Select the users that have the ID between 10 and 100, if their last name begins with "Smi" and order them by first name:
my $users = $schema->resultset("Users")->search({
 id => {-between => [10, 100]},
 last_name => {-like => "Smi%"},
}, {
 order_by => "last_name",
});

#Then add another condition to the previous search and select only the users that have their email starting with a given word ($email):
$users = $users->search({email => {-like => "$email%"}}) if $email;

#print the first name and email of all the found users (only at this step the database is accessed):
while (my $row = $users->next) {
 print $row->first_name, "\n";
 print $row->email, "\n";
}

#Delete the found users:
$users->delete;

#You can also update a found user using:
$user->update({
 email => '...',
});

And there are many other methods available.

If the users in the table user are commercial agents and they have their own corresponding clients in the table client, this relation beeing defined in the database using foreign keys, the generated DBIx::Class schema will see this as a "has_many" relation And in this case, here is a way to get the list of agents for each client if the has_many relationship is named "agents":

while (my $user = $users->next) {
 print $user->first_name, "\n";

 my $agents = $user->agents;
 while (my $agent = $agents->next) {
   print $agent->name, "\n";
 }
}

Of course, you can do all these things even in a Template-Toolkit template file and the database won't be access until that template file needs to be displayed:

[% WHILE (user = users.next) %]
 [% user.first_name %]

 [% agents = user.agents %]
 [% WHILE (agent = agents.next) %]
   [% agent.name %]
 [% END %]
[% END %]

Catalyst framework offers some features that makes it even more simple to use DBIx::Class.

HTH.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to