PHP ORM: Object-Relational Mapping for PHP

Tutorials

Connect To Your Database The method to connect to your database is simple; review any of the generated class files for additional examples. The include paths may need to be adapted for your application; the include paths in this example assume that you are querying from a file in your web application root.
<?php
require_once 'classes/databaseConnection/QueryRunner.php.inc';
$queryRunner = new QueryRunner();
$sql = 'SELECT now()';
$queryRunner->run_query($sql);
$qrs = $queryRunner->get_query_result_set();
$results = $qrs->get_results();
$time = '';
foreach($results as $row){
     $time = $row[0];
}
echo("The current time is $time");
?>

This produces the output: The current time is 2010-07-31 08:10:20
Select a List of Objects Selecting a list of Objects using specific criteria is a very common web application task. This example shows the ease with which this task can be completed using PHP ORM. For this example, reference the users sample table. Let's find all of the users with an email address at bestCompany.net
<?php
require_once 'classes/databaseConnection/QueryRunner.php.inc';
require_once 'classes/Users.php.inc';
$queryRunner = new QueryRunner();
$sql = 'SELECT id_user FROM users WHERE email LIKE ?';
$params = array();
array_push($params,'%bestCompany.net');
$queryRunner->run_query($sql,$params);
$qrs = $queryRunner->get_query_result_set();
$results = $qrs->get_results();
$users = array();
foreach($results as $row){
     array_push($users,new User($row['id_user']));
}
foreach($users as $user) {
     $user->to_string();
}
?>

unique_identifier -> 2
id_user -> 2
first_name -> Bob
last_name -> Cooper
email -> bob.cooper@bestCompany.net
databaseEnvironment ->


unique_identifier -> 3
id_user -> 3
first_name -> William
last_name -> Parker
email -> liam.cooper@bestCompany.net
databaseEnvironment ->


unique_identifier -> 5
id_user -> 5
first_name -> Sarah
last_name -> Jones
email -> sarah.jones@bestCompany.net
databaseEnvironment ->


Make Changes to Objects Making changes to a PHP ORM object is just as simple as loading that object. Consult the previous example and look at the user William Cooper in the last example. Let's change his email address to william.cooper@bestCompany.net: <?php
require_once 'classes/databaseConnection/QueryRunner.php.inc';
require_once 'classes/Users.php.inc';
$queryRunner = new QueryRunner();
$sql = 'SELECT id_user FROM users WHERE email=?'; // assume that we can select only the correct user this way!
$params = array();
array_push($params,'liam.cooper@bestCompany.net');
$queryRunner->run_query($sql,$params);
$qrs = $queryRunner->get_query_result_set();
$results = $qrs->get_results();
$user = NULL;
foreach($results as $row){
     $user = new User($row['id_user']);
}

if(!is_null($user)){
     $user->set_email('william.cooper@bestCompany.net');
     $user->store();
} else {
     die('The user wasn\'t loaded!');
}
Thank you for your interest in PHP ORM! This framework is currently under development and is not yet ready for the prime-time. Feel free to browse the documentation and check back soon for updates to the tools, documentation, example uses, etc.