Slide 1

Slide 1 text

Zend\Db in ZF 2.0 1

Slide 2

Slide 2 text

Who Am I? •Ralph Schindler (ralphschindler) Software Engineer on the Zend Framework team •At Zend for 4 years •Before that TippingPoint/3Com Programming PHP for 13+ years Live in New Orleans, LA. •Lived in Austin, Tx for 5 years 2 2

Slide 3

Slide 3 text

ZF 2.0 •Next generation of Zend Framework •Embrace 5.3 (and 5.4 in some places) •Embrace multiple programming paradigms AOP Event driven programming •More SOLID http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) More interfaces, more possibility for extension Practice dependency injection •More Agile and open! No more CLA Code on github.com 3 3

Slide 4

Slide 4 text

Zend\Db first pass... •Converted to namespaces •Converted to new Exception standards •Then, left alone while other things were developed on •Rewrite initial release in place of old Zend\Db during beta3 March 2012 4 4

Slide 5

Slide 5 text

Zend\Db Requirements In Short •A more clear and concise API (single responsibility) •Open for extension (open/close principle) •Practice dependency injection •Favor composition over inheritance •Do not solve Model Domain problems 5 5

Slide 6

Slide 6 text

What does that mean in practice •No statics anywhere 1 Exception: •class Zend\Db\TableGateway\Feature\GlobalAdapterFeature •Lots of (simple) interfaces Construction Free / Dependency Free Interfaces •Zend\Db\Adapter can be treated like a collaborator (dependency) in other components Zend\Db\TableGateway Zend\Db\Sql Zend\Db\Metadata ... Zend\Db\ActiveRecord? 6 6

Slide 7

Slide 7 text

What does that mean in practice, cont. •Very few Abstract classes ... Only in places where it is clear there is some shared implementation details •No final keyword on classes •No privates inside classes •All API are database centric, not model centric columns rows tables schemas 7 7

Slide 8

Slide 8 text

Walking through Zend\Db •Under the Zend\Db Namespace: Adapter ResultSet Sql TableGateway RowGateway Metadata 8 8

Slide 9

Slide 9 text

Insert->Header & Footer Zend\Db\Adapter\Adapter & Zend\Db\Adapter\ResultSet 9 9

Slide 10

Slide 10 text

Zend\Db\Adapter •Zend\Db\Adapter is a namespace •Zend\Db\Adapter\Adapter is the “kernel” of Zend\Db •Zend\Db\Adapter\* are all the interfaces and implementation details of the Adapter 10 10

Slide 11

Slide 11 text

https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Adapter.php 11

Slide 12

Slide 12 text

Two Primary Parts of Zend\Db\Adapter •Driver •Platform 12 12

Slide 13

Slide 13 text

Driver Responsibilities •Connecting to proper PHP extension •Reporting the capabilities of the extension •Coordinating 3 primary areas of interaction from driver object: Connections Statements Results 13 13

Slide 14

Slide 14 text

Driver continued •Connection Interface wrapper for connection resource and/or functions 14 14

Slide 15

Slide 15 text

Driver continued •Statement interface wrapper for statement resource and/or functions 15 15

Slide 16

Slide 16 text

Driver continued •Result interface wrapper for result resource and/or function 16 16

Slide 17

Slide 17 text

Platform •Responsible for: Knowing how to quote in a vendor RDBMS specific way Knowing the "name" of the database in a code-neutral way 17 17

Slide 18

Slide 18 text

Creating an Adapter •The really hard and explicit way ... 18 18

Slide 19

Slide 19 text

Creating An Adapter •The Easy and configuration driven way ... 19 19

Slide 20

Slide 20 text

Connection abstraction •first parameter of Adapter takes an array •Keys: driver, database, hostname, port, username, password, (pdodriver) any key you might find in php.net documentation 20 20

Slide 21

Slide 21 text

Adapter Convenience •Convenience API 21 21

Slide 22

Slide 22 text

Zend\Db\ResultSet •A collection of Rows •Model iteration in an Adapter/Driver neutral way •Present Rows as objects or arrays •There are A default concrete implementation ResultSet An abstract implementation for iterating common row sets An interface that enforces the Prototype pattern 22 22

Slide 23

Slide 23 text

23

Slide 24

Slide 24 text

24

Slide 25

Slide 25 text

25

Slide 26

Slide 26 text

Insert->Header & Footer Zend\Db\Sql 26 26

Slide 27

Slide 27 text

Zend\Db\Sql •Abstraction layer for creating DML (Data Manipulation Language in SQL) Select, Insert, Update, Delete •2 Modes Statement preparation SQL string generation 27 27

Slide 28

Slide 28 text

Zend\Db\Sql cont. •Serializable, clone-able objects Contain no references to adapter or platform objects •Preform Adapter specific parameterization Does the driver use ? or does it use :name? •Abstraction for DDL (Data Definition Language in SQL) (not completed yet, planned for 2.1) 28 28

Slide 29

Slide 29 text

Zend\Db\Sql\Insert, Update, Delete •Insert: 29 29

Slide 30

Slide 30 text

Zend\Db\Sql\Insert, Update, Delete •Update: 30 30

Slide 31

Slide 31 text

Zend\Db\Sql\Insert, Update, Delete •Delete 31 31

Slide 32

Slide 32 text

Zend\Db\Sql\Select •Object for building queries •columns, joins, where, offset, limit •Ability to track identifiers and values in vendor and driver neutral way •Fluent API (LINQ style) 32 32

Slide 33

Slide 33 text

Zend\Db\Sql\Select 33 about 30 or so of these reserved for later (demo time)... 33

Slide 34

Slide 34 text

Zend\Db\Sql\Expression •Query/Statement agnostic value and identifier placeholders Uses “?” as common placeholder •Object to use for “pass through” of SQL fragments Much like Zend_Db_Expr in ZF1 34 34

Slide 35

Slide 35 text

Zend\Db\Sql\Where & Predicates •Zend\Db\Sql\Where models SQL WHERE statements Is the first node of a tree •Zend\Db\Sql\Predicate are SQL predicates http://en.wikipedia.org/wiki/Where_(SQL) Truth values Combined by parenthesizes, AND and OR Predicate sets can be nested: •by API: $predicateSet->andPredicate($predicate); •or via Fluent: – $where->NEST->like(‘name’, ‘Ralph%’)->UNNEST; •example to follow in later slides 35 35

Slide 36

Slide 36 text

This actually is the interface for where, as class Where extends Predicate {} ... 36

Slide 37

Slide 37 text

Supported Predicates •Between •Expression SQL Fragment / Specialized functions, etc •In •IsNull •Like •Operator (>, <, =, <>, >=, <=) •Will explore more in Demo, if time permits https://github.com/zendframework/zf2/tree/master/library/Zend/Db/Sql/Predicate 37 37

Slide 38

Slide 38 text

SQL Abstraction •Zend\Db\Sql\Sql is how you attain SQL abstraction according to the specific adapter / platform you are attempting to run against. They can be bound to a table Is used to take Select, Insert, Update, Delete objects and prepare or generate SQL for. 38 38

Slide 39

Slide 39 text

39

Slide 40

Slide 40 text

LIMIT OFFSET EXAMPLE 40

Slide 41

Slide 41 text

LIMIT OFFSET EXAMPLE 40

Slide 42

Slide 42 text

Insert->Header & Footer Zend\Db\TableGateway Zend\Db\RowGateway 41 41

Slide 43

Slide 43 text

Zend\Db\TableGateway •Table Data Gateway Pattern •http://martinfowler.com/eaaCatalog/tableDataGateway.html •“An object that acts as a Gateway to a database table. One instance handles all the rows in the table.” 42 42

Slide 44

Slide 44 text

TableGateway Interface •TableGatewayInterface •TableGateway Implementations: AbstractTableGateway TableGateway 43 43

Slide 45

Slide 45 text

TableGateway 44

Slide 46

Slide 46 text

45

Slide 47

Slide 47 text

Zend\Db\RowGateway •Row Data Gateway •http://martinfowler.com/eaaCatalog/rowDataGateway.html •“An object that acts as a Gateway to a single record in a data source. There is one instance per row.” 46 46

Slide 48

Slide 48 text

RowGateway Interface 47 47

Slide 49

Slide 49 text

48

Slide 50

Slide 50 text

Insert->Header & Footer Zend\Db\Metadata 49 49

Slide 51

Slide 51 text

Zend\Db\Metadata •Component capable of interrogating a database for schema information •Describe schema to consumers in a vendor neutral way •Cacheable and serializable 50 50

Slide 52

Slide 52 text

Metadata Interface 51 51

Slide 53

Slide 53 text

Insert->Header & Footer Learn By Example 52 52

Slide 54

Slide 54 text

Examples •Zend\Db\Sql\Select Examples https://gist.github.com/3949548 •Working example repository: https://github.com/ralphschindler/Zend_Db-Examples/ Follow instructions there to setup working PDO sqlite file •essentially just: php setup/up.php •Another Framework-less project using Zend\Db https://github.com/ralphschindler/PatternsTutorialApp Explored Modeling with Zend\Db •Repository •Mapper •Entity •Data Mapper consuming Zend\Db\TableGateway 53 53

Slide 55

Slide 55 text

Future •2.1 Db2 Adapter Oracle Adapter •2.1 or 2.2 DDL generation •2.2+ (If there is any demand) ActiveRecord using static inheritance 54 54

Slide 56

Slide 56 text

Insert->Header & Footer Thanks! http://twitter.com/ralphschindler http://framework.zend.com/zf2 http://github.com/zendframework/ http://github.com/ralphschindler 55 55