Hussein Harake

The MySQL Connection Manager for classic (no-ORM) queries

Posted in CakePHP by harake on January 30, 2010

Singleton And Connection Manager

Singleton: one class that (via itself) creates one and only one instance (of itself) if that instance was not created before, and always returns that instance.

ConnectionManager: Class that acts as a a set of singletons, so it creates for each database account one and only one singleton.

The default database account in CakePHP is ‘default’ and it is is of type mysql.

Since most of PHP developers are fans of MySQL (both the mysql and mysqli extensions), I created the class below, to show you how to integrate your old Mysql Connection Manager into the CakePHP framework.

Mysql Connection Manager Class

Downloaded the Mysql Connection Manager From http://github.com/Harake

(I personally put the class in “app\libs\mysql” folder and name it as “mysql_connection_manager.php”)


The Changes in your own “app\config\bootstrap.php”

Just add this function

function cn($account = 'default') {
  if (!class_exists('MysqlConnectionManager')) {
    include_once APP . 'libs' . DS . 'mysql' . DS . 'mysql_connection_manager.php';
  }
  return MysqlConnectionManager::connect($account);
}

Where and how to use it

In general, it can be used anywhere in your application like this

/*

when you call $c = cn();

you are sure that a MySQL connection is created

if it was not existing (singleton pattern)

*/

$c = cn();
if ($c) {
 //...
 $id = $c->loadValue("SELECT MAX(id) FROM users WHERE status = 1");
 $ids = $c->loadColumn("SELECT id FROM users WHERE status = 1");
 $user = $c->loadSingle("SELECT * FROM users WHERE status = 1 LIMIT 1");
 $users = $c->load("SELECT * FROM users WHERE status = 1");
 //... and many more...just read the class to discover
}

Please leave a comment, If you like this article or have one or more remark(s) on it

function cn($account = ‘default’) {
if (!class_exists(‘MysqlConnectionManager’)) {
if (floatval(Configure::read(‘Cake.version’)) < 1.3) {
include_once APP . ‘libs’ . DS . ‘mysql’ . DS . ‘mysql_connection_manager.php’;
} else {
App::import(‘Lib’, ‘mysql’ . DS . ‘mysql_connection_manager’);
}
}
return MysqlConnectionManager::connect($account);
}
Advertisement

6 Responses

Subscribe to comments with RSS.

  1. Jamel said, on February 5, 2010 at 10:28 pm

    Hey man, don’t you think this gonna break MVC, while it is better to stick to cake conventions?

    It is working does not mean recommended as you know!

  2. harake said, on February 5, 2010 at 10:58 pm

    yes you are right “working does not mean recommended”.

    But I do not agree with you that you should stick to cake conventions.

    Sometimes instead of loading some stand-alone models, I prefer using the MysqlConnectionManager.

    It is also useful for Custom pagination queries.

    It is very fast… try it

  3. Jamel said, on February 5, 2010 at 11:18 pm

    still not convinced, please give me a concrete example, where sticking to cake convention is something of madness!

  4. harake said, on February 6, 2010 at 12:03 pm

    Ok!

    for some statistical or security reasons you are logging the differnet IP addresses of the clients requests into a table called “ips” which has only “id” and “name” fields.
    the scenario is if the record of the current request IP address is already existing, returns its id, otherwise creates the record and returns the new id.

    Do you think it is convenient to create a model for this issue while you know that you will not (at any rate) edit or create IP records using forms? Remember That this action should be taken for every client request. What if you need to log also the user agent of every client request into a table called user_agents having only “id” and “name” fields for the same strategy “create the record if it was not existing and always return the id”.

  5. Joselyn Bachtold said, on March 11, 2010 at 7:51 pm

    Just landed on this post via Google seek. I love it. This situation switch my perception and I am bringing the RSS feeds. Cheers.

  6. harake said, on March 12, 2010 at 12:04 pm

    Joselyn,
    Thank you!
    Cheers!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.