accelerating php development data-tier programming brian rosenthal zvi boshernitzan robocommerce
Post on 12-Jan-2016
213 Views
Preview:
TRANSCRIPT
Overview: Data modeling
High-level approach to database design. We will describe
Basic principles of data modeling PHP Programming techniques
Database abstractions Stack-based database programming Inference-based database programming
Basic principles of data modeling Normalization: “Don’t repeat yourself.” Entity Relation “ER” diagrams
Normalization“Don’t Repeat Yourself” First approach: store
the author information within the book.
Problem: what if there is more than one author. Where do we store the second author?
-title-isbn-author_first_name-author_last_name
Books
Normalization“Don’t Repeat Yourself” Second approach:
store up to three authors
Problems: what if there are more, and what about all of the information about the authors (address, etc.)
-title-isbn-author1_first_name-author1_last_name-author2_first_name-author2_last_name-author3_first_name-author3_last_name
Books
Normalization“Don’t Repeat Yourself” Third approach: have
two tables: books and authors.
Authors have a book id.
“Each book may be written by 0 or more authors.”
Each author must be the writer of a book.
-title-isbn
Books
-book_id-first_name-last_name
Authors
-written by1
-writer of0..*
Normalization“Don’t Repeat Yourself”
Books
id title
1 Da Vinci Code
2 Angels and Demons
Authors
id book_id first_name last_name phone_number
1 1 Dan Brown 800-808-0808
2 2 Dan Brown 800-808-0808
Problem: We still have redundancy
Normalization“Don’t Repeat Yourself” Fourth approach:
have three tables: books, people, authorship.
Each entry in the “authorship” table is a fact. That a particular person participated in an authorship of a book
No information is repeated!
-title-isbn
Books-first_name-last_name-phone_number
Persons
-work of1
-of0..*
-book_id-author_id
Authorship
-participate in1
-of0..*
ER Diagrams This is an ER diagram. You read it: “Each product must be sold under a
brand” “Each brand may be the origin of 0 or more
products”category
product
brand
-category for1
-categorized by0..*
-origin of1
-sold under0..*
keyword
keyword_products
-indexed by1-for0..*
-indexed of1
-on0..*
PHP Database Infrastructure
Zvi: database abstractions. Brian: stack-based SQL programming Brian: inference-based SQL programming
Database “stack” programming SQL as a big string versus SQL as a “stack” SQL is, after all structured. {‘select’:’firstname,lastname’, ‘from’:’customers c’, ‘ijoin’:{‘s’:{‘table’:’sites’, ‘on’:’s.id = c.site_id’}}, ‘where’:[“c.name like ‘%brian%’”, “c.id > 10”], ‘limit’:10, ‘offset’:5} $q->add_where(…) $q->add_select(…), $q->set_select(…) $q->add_lojoin(…) $q->load_dict({‘select’:’c.address_id’, ‘where’:…}) $q->to_sql() $q->to_dict()
SPEC DRIVEN DATABASE PROGRAMMING db::objects($q->to_sql()) db::assocs($q->to_sql()) db::string($q->to_sql()) db::object($q->to_sql())
INFERENCE-BASED DATABASE PROGRAMMING A lot of information is stored in the database
schema The idea here is to use it where it is possible
to do so
Inferences from ER diagrams… if you were writing classes to manage these database entities, you might include:
products::mget_by_category(…) products::mget_by_brand(…) products::map_keyword(…) products::mget_by_keyword(…) keywords::mget_by_product(…)
category
product
brand
-category for1
-categorized by0..*
-origin of1
-sold under0..*
keyword
keyword_products
-indexed by1-for0..*
-indexed of1
-on0..*
Enter: PHP 5
Call handler override
“function __call($m, $args)” Mysql reflection Singleton / factory semantics Autoloads
You should really get all of these functions for free:
mg(‘products’)->get($product_id)
mg(‘products’)->create(‘name’, ‘lawnmower’)
mg(‘products’)->mget_by_keyword($keyword_id)
mg(‘products’)->mget_by_category($category_id)
mg(‘products’)->mget_by_brand($brand_id)
mg(‘products’)->get_by_sku($sku)
mg(‘products’)->get_title($product_id)
mg(‘products’)->get_title(‘sku’, $sku)
mg(‘products’)->map_keyword($keyword_id)
mg(‘products’)->colnames()
mg(‘categories’)->get_products($category_id)
So, here’s the class:class products extends dbentity {
function references() {
return array('brand_id' => 'brands‘, 'category_id' => 'categories');
}
function mappings() {
return array(
‘keywords' => array(‘product_keywords', 'product_id', ‘keyword_id', ‘keywords‘)
, ‘related_products' => array('product_relations', 'from_product_id', 'to_product_id', 'products')
, 'suppliers' => array('supplier_prices', 'product_id', 'supplier_id', 'suppliers‘
, array('wholesale_price' => 'price', 'sku' => 'sku'))
);
}
}
Customizing the sql:$q = new dbquery(array(
‘select’ => ‘p.name’
, ‘from’ => ‘products p’
, ‘ijoin’ => array(‘c’ => rba::_(‘table’, ‘categories’
, ‘on’, ‘c.id = p.category_id’))
, ‘where’ => “name like ‘%lemon%’ ”
, ‘order_by’ => ‘p.name’
));
$q->add_select(‘p.subname’)
$q->add_ijoin(…)
$q->set_limit(…)
$q->load($q_spec)
Customizing the SQL
$dbquery = array(
‘select’ => ‘c.name as category_name’
, ‘ijoin’ => array(‘c’ => rba::_(‘table’, ‘categories’
, ‘on’, ‘c.id = p.category_id’)))
mg(‘products’)->get(product_id, compact(‘dbquery’))
Objectives/ Scope
Business Owner’s View
Architect’s View*
Designer’s** View
Builder’s View***
Functioning System
List of Important
Things
Terms, Definitions
Entity/ Relationship
Diagram
Tables, Classes
Data, physical storage design
List of Processes
Business Process Model
Essential Functions
System Design
Detailed Program Design
Business Locations
Operationsby Business
Location
Data Links, Processing Locations
Network Architecture
(h/w, s/w types)
Network Construction
Organi- zational
Units
Org. Chart, Roles
Roles+Data
(Use Cases)
User Interface, Security
Screens, Security Design
Business Events, Cycles
Master Business Schedule
State/ transactions,
ELH
“Control Flow”
diagrams
Timing Definitions
Business Vision and Mission
Business Policies and
Rules
Business Rule Model
Rule Design
Rule Specification
Working System
Network(Where)
Data(What)
Activities(How)
People(Who)
Timing(When)
Motiva-tion (Why)
* John Z calls this the “information designer’s” perspective.
** He calls this the “builder’s” view.
*** This is John Z’s “sub-contractor’s” view.
Zachman Framework
top related