Navid_pdp11
Navid_pdp11

Reputation: 4012

how to create an data repository for idiorm?

I am new in using Idiorm as a php light weight ORM, now i want to create a data repository class which share ORM between multiple data access classes. in simpler way to describe I want to configure ORM and connect to database one time and use it until my application ends.

how can do this?

Upvotes: -1

Views: 156

Answers (2)

gview
gview

Reputation: 15361

The number of connections is a low level attribute of whatever database is being used.

In the case of PDO, you would do something like this:

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));
?>

In the section of the [Idiorm manual][1] on PDO options it has this example for passing an option to PDO:

<?php
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

So it stands to reason that you can do this with idiorm:

<?php
ORM::configure('driver_options', 
   array(PDO::ATTR_PERSISTENT => true));

Upvotes: 0

alttag
alttag

Reputation: 1173

By choosing to use an ORM, you're giving up a great deal of control about duration of a connection and whether it's reused. That's the nature of an abstraction layer: a simplified interface in exchange for specific control.

In addition, if you're building a repository class, I'm not sure an ORM is a good choice. (From the previous link: "[T]he [repository] pattern is very popular ..., it is also frequently misunderstood and misused.") In some respects, and ORM already acts like a repository class.

In the end, it sounds like you may be recreating the wheel. Unless you have a specific need (i.e., to correct a problem you're actively experiencing), building a repository class in addition to using an ORM may not be worth it. And unless you have active bugs that require you to manage your database connections closely, you almost certainly don't want the hassle of managing them explicitly. Create connections when you need them, drop them when you don't. Even better, let the ORM handle it for you.

It sounds like you're adding a great deal of complexity to a project that you almost certainly aren't going to need.

Upvotes: 0

Related Questions