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

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View 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 Slide

  21. Adapter Convenience
    •Convenience API
    21
    21

    View 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 Slide

  23. 23

    View Slide

  24. 24

    View Slide

  25. 25

    View Slide

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

    View Slide

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

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

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

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

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

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

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

    View Slide

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

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

  39. 39

    View Slide

  40. LIMIT OFFSET EXAMPLE
    40

    View Slide

  41. LIMIT OFFSET EXAMPLE
    40

    View Slide

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

    View Slide

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

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

    View Slide

  45. TableGateway
    44

    View Slide

  46. 45

    View Slide

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

  48. RowGateway Interface
    47
    47

    View Slide

  49. 48

    View Slide

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

    View Slide

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

  52. Metadata Interface
    51
    51

    View Slide

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

    View Slide

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

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

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

    View Slide