2017 © Pedro Peláez
 

library oodb

Easy Object Oriented Database Handler, like mongoDB.

image

dral/oodb

Easy Object Oriented Database Handler, like mongoDB.

  • Saturday, September 13, 2014
  • by dralletje
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

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., (*1)

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., (*2)

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, (*3)

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

But people with a lower PHP version should use, (*4)

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

Note that creating the handler differs from creating a mysqli handler, which is, (*5)

$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., (*6)

$table = $database->tablename;

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

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), (*8)

id (auto incement) firstname Lastname Age

But, the table is empty! Let's add some info!, (*9)

$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., (*10)

Now the table looks like this:, (*11)

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

I now magically added some more:, (*12)

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, (*13)

$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., (*14)

Imagine you want to remove everybody older than 18 years old. That would be done like this:, (*15)

/* 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, (*16)

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

Documentation

Update: You can use update using, (*17)

$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, (*18)

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

I'm writing a api reference soon :), (*19)

The Versions

13/09 2014

dev-master

9999999-dev https://github.com/dral/OODB

Easy Object Oriented Database Handler, like mongoDB.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

database nosql mysql mongo oop oo