Skip to content

Instantly share code, notes, and snippets.

@romaninsh
Created October 24, 2016 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romaninsh/c28ceb0ee38918fc0d5af8b96d7d7910 to your computer and use it in GitHub Desktop.
Save romaninsh/c28ceb0ee38918fc0d5af8b96d7d7910 to your computer and use it in GitHub Desktop.
PHP Data Libraries
<?php
// This file contains comments that help you understand comparison
// criteria better: for "PHP Data Library comparison"
// http://socialcompare.com/en/comparison/php-data-access-libraries-orm-activerecord-persistence
// General Premise
// Data Access frameworks hide implementation detail of a specific database engine and allow you to work
// with data in an easier way. See also "Domain Model", "Persistence" and "Mapping".
// Legend
// GREEN: feature is natively supported or can be achieved in 1-2 lines of code or add-on.
// YELLOW: planned, has work-around or not relevant to the approach.
// RED: not supported or requires a hack.
// Entity Definition, using pseudo PHP:
class Client {
public string $name; // label: Full Name
public text $address;
public bool $vip;
public $invoices[];
}
class Invoices {
public Client $client;
public int $total;
public $lines[];
}
class Line {
public money $price;
public int $qty;
public money $total;
}
// Persistence Definition
$db = new Persistence_SQL($dsn);
$data = [];
$db_array = new Persistence_Array($data);
$db_mongo = new Persistence_MongoDB($connection);
$json = "[]";
$db_json = new Persistence_JSON($json);
/////////////////////////////////////////////////////////////////////////////////
// Same model, multiple persistences
/**
* Persistence-agnostic code
*/
function loadUser($user_model) {
return $user_model->loadBy('name', 'John');
}
$user_sql = new User($db);
$user_array = new User($db_array);
//////////////////////////////////////////////////////////////////////////////////
// Vendor-specific extensions
function getStats($user_model) {
if ($user_model->persistence instanceof Persistence_Mongo) {
$user_model->aggregate(...);
} else {
foreach ($user_model ..) {
..
}
}
}
////////////////////////////////////////////////////////////////////////
// Table Name, Field, etc mapping
$user_sql = new User($db, 'app_user_table');
$user_sql->name.actual_sql_column = 'full_name';
echo $user_sql->name; // shows full_name.
////////////////////////////////////////////////////////////////////////
// Map Entity to SubQuery
$user.table = 'call get_user_list()';
////////////////////////////////////////////////////////////////////////
// Native field mapping
var_dump($user->birth_date); // <-- DateTime object
///////////////////////////////////////////////////////////////////////
// User Defined Types
$line->total = [259.29, 'EUR'];
$line->save();
// stores into "total_amount" and "total_currency"
///////////////////////////////////////////////////////////////////////
// related Entity field
$invoice->load(2); // single query
echo $invoice->client->name; // no extra query!
///////////////////////////////////////////////////////////////////////
// sub-query on Related Entity
$invoice->load(2);
echo $invoice->total; // sum(amount) from line where invoice_id = 2
///////////////////////////////////////////////////////////////////////
// Entity to join multiple tables
$client->load(2);
$client->name = 'John';
$client->address = '29 Street';
$client->save();
// UPDATE client set name = 'John'
// UPDATE client_address set address = '29 Street'
// Criteria is a permanent condition on a domain model, that must
// be true at all times.
////////////////////////////////////////////////////////////////////////
// Soft Delete
$client->load(2); // record not found. Actually it's there but marked as deleted.
// criteria condition didn't match.
////////////////////////////////////////////////////////////////////////
// Domain Model Criteria
$vip_client = new VipClient();
$vip_client->name='Peter';
$vip_client->save();
// INSERT into client name='Peter', vip=1, deleted=0
////////////////////////////////////////////////////////////////////////
// Criteria compliance
$vip_client = new VipClient();
$vip_client->name='Peter';
$vip_client->vip = false;
$vip_client->save(); // fails! only vip clients!
////////////////////////////////////////////////////////////////////////
// Expression conditions
class ClientWithCondition {
public $name;
public $surname;
condition $name=$surname;
}
$c = new ClientWithCondition();
$c->load(2); // where id = 2 and name = surname
/////////////////////////////////////////////////////////////////////////
// Dynamic Condition
$id = $_GET['order_id'];
$c = new AccessibleOrder();
$c->load($id);
// where id = 2 and client_id = 39 <-- due to user authentication
/////////////////////////////////////////////////////////////////////////
// Convert Model into Query
$order = new AccessibleOrder();
$query = $order->getUpdateQuery();
$query->set('is_shipped', false)->update();
// update order set is_shipped where client_id = 39 <-- due to authentication
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment