Skip to content
/ OODB Public

An object orient database class, now primairy for mysql

Notifications You must be signed in to change notification settings

dralletje/OODB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Object Oriented Database

Bored of all the semi object oriented database handlers in php,
and inspired by the fully object oriented working of mongoDB in nodejs, I made OODB.
It's not a real database, just a class to access them easily and in a readable way.

Creating the connection and the table

You just import the OODB main class.
This will help you to import the database of the type you want to use.

include('OODB/OODB.php');

Now you are ready to instantiate the database handler.
In this example I'm connecting to a Mysql database. As of PHP 5.3 you can use

$database = OODB::mysql($host, $database, $user, $password);

But people with a lower PHP version should use

$database = OODB::get("mysql", $host, $database, $user, $password);

Note that creating the handler differs from creating a mysqli handler, which is

$database = new mysqli($host, $user, $password, $database); # just the order of arguments

After you created the handler, you can select a table in the database.

$table = $database->tablename;

Yes, it actually is that easy! But this is not even te most awesome part.

Getting, setting, deleting

This, is where the real power starts. Let's say we aready connected to the database, and chose a table as we did in the previous section and we have already set up an table with the following structure (You can't create tables with this wrapper yet)

id (auto incement) firstname Lastname Age

But, the table is empty! Let's add some info!

$table->insert(array(
  'firstname' => 'Markus',
  'lastname' => 'Persson',
  'Age' => 42
));

That's it! Now this info is inserted. There is no restriction in what order you add the arguments. Just use the name. The script automaticly sees 'id' is auto_increment, so it won't give you an error. The script also prevents any form of mysql injections, by using bind_param automaticly. When you try to insert a non existing column, it will give you an instructive error.

Now the table looks like this:

id (auto incement) firstname Lastname Age
0 Markus Person 42

I now magically added some more:

id (auto incement) firstname Lastname Age
0 Markus Person 42
1 Barrack Obama 51
2 Michiel Dral 16

Right, but what if I want to find the age of Markus Persson back! Let's see what we can do

$result = $table->find(array(
  'firstname' => 'Markus',
  'lastname' => 'Persson',
));

if(count($result) !=== 1) {
  die("None, or more than one found? Strange..");
}

$markus = $result[0];
echo "The age of ".$markus['firstname']." ".$markus['lastname']." is ".$markus['age'];

Easy as that! I know the $markus['firstname'] and lastname are a bit expendable, but I just want to show how all data get's into a clean, easy to use array.

Imagine you want to remove everybody older than 18 years old. That would be done like this:

/* Note how I use $database here again, it's for easier comparisons */
$table->delete(array(
  'age' => $database::gte(18)
));

Or without the $database variable, it works like mongodb

$table->delete(array(
  'age' => array('$gte' => 18)
));

Documentation

Update: You can use update using

$table->update($where, $new);

So in the previous example, if I would like to set the age of everybody with the firstname "Markus" to 1337, I would do You can also update using

$table->update(array(
  'firstname' => 'Markus'
), array(
  'age' => 1337
));

I'm writing a api reference soon :)

About

An object orient database class, now primairy for mysql

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages