Skip to content

Instantly share code, notes, and snippets.

@alganet
Created March 28, 2011 01:45
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 alganet/889861 to your computer and use it in GitHub Desktop.
Save alganet/889861 to your computer and use it in GitHub Desktop.
Use cases for Respect\Relational
<?php
/**
* Infered database schema from naming conventions.
* Mapping works without configuration, reflection, annotation, reverse engineering, show tables
* or anything like that at all. All samples below generate a single SQL statement with apropriated joins.
*
* -All tables have a primary key named "id"
* -All foreign keys are named table_id
* -All association tables (N to N) are named table_table
*
* CREATE TABLE developer (
* id INT PRIMARY KEY,
* name VARCHAR(32)
* );
* CREATE TABLE bug (
* id INT PRIMARY KEY,
* title VARCHAR(32),
* developer_id INT REFERENCES developer(id)
* );
* CREATE TABLE bug_developer (
* id INT PRIMARY KEY,
* bug_id INT REFERENCES bug(id),
* developer_id INT REFERENCES developer(id)
* );
*/
$m = new Mapper(new Schemas\Infered());
$dev = $m->find('developer', 21)->fetch(); //find developer by primary key
$dev = $m->developer[21]->fetch(); //same as the above
echo $dev->name; //show retrieved developer name
$bugs = $m->find('bug')->find('developer', 21)->fetchAll(); //fing bugs by developer 12
$bugs = $m->bug($m->developer[21)->fetchAll(); //same as above
$bugs = $m->bug->developer[21]->fetchAll(); //same as above
echo $bugs[0]->developer->name; //show the developer name from the first bug found
//find developers who are associated with bugs created by the developer 33
$devs = $m->developer->bug_developer->bug->developer[33]->fetchAll();
echo $devs[0]->bug->developer->name; //show the name of the first developer associated
/**
* Reverse Engineered Schema from statements like SHOW TABLES, SHOW COLUMNS, etc.
* No naming conventions required. All samples below generate a single SQL statement with apropriated joins.
*
* CREATE TABLE developer (
* id INT PRIMARY KEY,
* name VARCHAR(32)
* );
* CREATE TABLE bug (
* id INT PRIMARY KEY,
* title VARCHAR(32),
* reported_by INT REFERENCES developer(id),
* assigned_to INT REFERENCES developer(id)
* );
* CREATE TABLE bug_watch (
* bug_id INT PRIMARY KEY REFERENCES bug(id),
* developer_id INT PRIMARY KEY REFERENCES developer(id)
* );
*/
$m = new Mapper(new Schemas\ReverseEngineered());
//find developers who are watching with bugs assigned to the developer 33
$devs = $m->developer->bug_watch->bug->assigned_to[33]->fetchAll();
echo $devs[0]->bug->developer->name; //show the name of the first developer associated
/**
* Schema Decorators (works with any schema infered or reverse engineered). Adds funcionality to
* schema providers like naming inflection, lazy loading, schema caching, etc.
*
* Sample below converts database names from namespace_separated to camelCased.
*/
$m = new Mapper(new Schemas\Inflected(new Schemas\ReverseEngineered()));
//find developers who are watching with bugs assigned to the developer 33
$devs = $m->developer->bugWatch->bug->assignedTo[33]->fetchAll();
echo $devs[0]->bug->developer->name; //show the name of the first developer associated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment