Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Zend Db In ZF2

Zend Db In ZF2

Slides for a talk I gave at ZendCon 2012.

Corresponding code located at https://github.com/ralphschindler/Zend_Db-Examples

Ralph Schindler

October 23, 2012
Tweet

More Decks by Ralph Schindler

Other Decks in Technology

Transcript

  1. Zend\Db in ZF 2.0
    1

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  21. Adapter Convenience
    •Convenience API
    21
    21

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. LIMIT OFFSET EXAMPLE
    40

    View full-size slide

  37. LIMIT OFFSET EXAMPLE
    40

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. TableGateway
    44

    View full-size slide

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

    View full-size slide

  43. RowGateway Interface
    47
    47

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  46. Metadata Interface
    51
    51

    View full-size slide

  47. Insert->Header & Footer
    Learn By Example
    52
    52

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide