Presentation is loading. Please wait.

Presentation is loading. Please wait.

Accelerating PHP development Data-tier programming Brian Rosenthal Zvi Boshernitzan Robocommerce.

Similar presentations


Presentation on theme: "Accelerating PHP development Data-tier programming Brian Rosenthal Zvi Boshernitzan Robocommerce."— Presentation transcript:

1 Accelerating PHP development Data-tier programming Brian Rosenthal Zvi Boshernitzan Robocommerce

2 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

3 Basic principles of data modeling Normalization: “Don’t repeat yourself.” Entity Relation “ER” diagrams

4 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?

5 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.)

6 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.

7 Normalization “Don’t Repeat Yourself” Books idtitle 1Da Vinci Code 2Angels and Demons Authors idbook_idfirst_namelast_namephone_number 11DanBrown800-808-0808 22DanBrown800-808-0808 Problem: We still have redundancy

8 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!

9 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”

10 PHP Database Infrastructure Zvi: database abstractions. Brian: stack-based SQL programming Brian: inference-based SQL programming

11 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()

12 SPEC DRIVEN DATABASE PROGRAMMING db::objects($q->to_sql()) db::assocs($q->to_sql()) db::string($q->to_sql()) db::object($q->to_sql())

13 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

14 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(…)

15 Enter: PHP 5 Call handler override “function __call($m, $args)” Mysql reflection Singleton / factory semantics Autoloads

16 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)

17 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')) ); }

18 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)

19 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’))

20 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 Operations by 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


Download ppt "Accelerating PHP development Data-tier programming Brian Rosenthal Zvi Boshernitzan Robocommerce."

Similar presentations


Ads by Google