The NotORM ORM

NotORM is a simple PHP library which is really an ORM. So one can work with a database in PHP as one would in LINQ within C#.

Lightweight Configuration

NotORM is very lightweight in terms of configuration and ease of use.

Here is a simple configuration for a MySQL database:

$dsn = 'mysql:dbname=finance;host:localhost';

$username = 'open-guy';
$password = 'secret';

$pdo = new PDO($dsn, $username, $password);
$db = new NotORM($pdo);

Abstract Calls

Database calls with NotORM work with SQL concepts of tables and rows. We have two tables: exchange and company.

Query

$companyId = '23hx7a';
// companyId the key for the exchange table
$list = $db->exchange()->where("id", $companyId);

// get the item
$item = $exchange->fetch();

// if item found construct an associative array
if ($item){
    $data = array(
        "status" => $item["status"],
        "company" => $item["company"],
        "price" => $item["price"]
    }
 }

Update

$companyId = '23hx7a';
$company = $db->company()->where("companyId", $companyId);
if ($company ->fetch()) {

    $putArray = array('status' => 'IPO');

    $result = $company->update($putArray);
    if ($result){
        echo json_encode(
            array(
                "code" => (bool)$result,
                "message" => "Company updated successfully",
            )
        );
    }
    else{
        echo json_encode(array(
            "code" => false,
            "message" => "Update failed"
        ));
    }   
}
else{
    echo json_encode(array(
        "code" => false,
        "message" => "No such company"
    ));
}

Port Across Database Servers

A major advantage of NotORM is the simplicity of converting across database servers by replacing a few configuration strings. So to move the code above from using MySQL to using MS SQL Server, all we need is to change these three lines:

 // connection string
 $dsn =  'sqlsrv:Server=example.database.windows.net;Database=finance';

// username and password
$username = 'ms-girl';
$password = 'can you keep a secret?';