Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Talking to the Database

on

  • 2,342 views

An introduction on using PDO to talk databases, as well as overviews of Zend_Db, PhpORM, and Doctrine

An introduction on using PDO to talk databases, as well as overviews of Zend_Db, PhpORM, and Doctrine

Statistics

Views

Total Views
2,342
Views on SlideShare
2,338
Embed Views
4

Actions

Likes
1
Downloads
32
Comments
0

1 Embed 4

http://www.scoop.it 4

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Talking to the Database Talking to the Database Presentation Transcript

  • Talking To The Database
      Chris Tankersley
    Joind.in Link: http://joind.in/2348 E-mail: [email_address] Twitter: @dragonmantank
  • Who Are You and Why Are You In My House? Chris Tankersley Been doing PHP for almost 8 years now Lots of projects no one uses released under the BSD license Contributer to the Habari Project
  • An entire talk on PHP ↔ DB? Yes
  • What will we gain? How to use PDO to make DB access easier Overview of ORMs (Object Relational Mappers)
  • A History Lesson
  • Old Skool PHP Hardcoded Everything 1 <?php 2 $conn = mysql_connect ( 'localhost' , 'root' , '' ); 3 mysql_select_db ( 'mydb' ) or die ( 'Bad Connection!' ); 4 5 $query = 'SELECT * FROM mytable WHERE id = ' . $_GET [ 'id' ]; 6 $result = mysql_query ( $query ); 7 8 while ( $row = mysql_fetch_assoc ( $result )) { 9 echo $row [ 'id' ]. ': ' . $row [ 'value' ]; 10 }
  • Then we got smarter config.php 1 define ( 'DB_HOST' , 'localhost' ); 2 define ( 'DB_USERNAME' , 'root' ); 3 define ( 'DB_PASSWORD' , '' ); 4 define ( 'DB_DATABASE' , 'mydb' ); 1 require_once ( 'config.php' ); 2 3 $conn = mysql_connect (DB_HOST, DB_USERNAME, DB_PASSWORD); 4 if ( $conn == null) { 5 die ( 'Unable to connect' ); 6 } 7 8 if (! mysql_select_db (DB_DATABASE)){ 9 die ( 'Couldnt connect to DB' ); 10 } db.php
  • Then we got smarterer Classes! 1 class DB { 2 var $conn ; 3 function DB () 4 { 5 $this ->conn = $this -> connect (DB_TYPE); 6 } 7 8 function connect ( $type = 'MYSQL' ) { 9 switch ( $type ) { 10 case 'MYSQL' : 11 return mysql_connect (DB_HOST, DB_USERNAME, DB_PASSWORD); 12 break ; 13 case 'POSTGRESQL' : 14 // ...
  • PHP Data Objects
    • Introduced in PHP 2004 with PHP 5.0
    • Enabled by Default since 2005 with PHP 5.1
    • Provides DB Abstraction
    • Safer (if used right)
  • What is PDO?
    • A Generic interface for working with databases
    • Supports a wide range of DB servers via extensions
      • MySQL
      • PostgreSQL
      • SQL Server
      • SQLite
      • http://www.php.net/manual/en/pdo.drivers.php
  • Find What Drivers are Available Check phpinfo(); Ask PDO: 1 foreach (PDO::getAvailableDrivers as $driver ) { 2 echo $driver . '<br>' ; 3 }
  • Let's Connect!
    • Connection Strings
      • They are extension specific
    1 try { 2 $dbh = new PDO ( 'mysql:host=localhost;dbname=mydb' , 'root' , '' ); 3 echo 'Connected!' ; 4 } catch (PDOException $e ) { 5 echo $e -> getMessage (); 6 } MySQL: 1 try { 2 $dbh = new PDO ( 'sqlite:/path/to/database.db' ); 3 echo 'Connected!' ; 4 } catch (PDOException $e ) { 5 echo $e -> getMessage (); 6 } SQLite:
  • Always Be Prepared
    • PDO can be used with or without prepared statements
    • Always use prepared statements
      • Preparing statements is the easiest way to avoid SQL injection
    • Binding can be done via name or order (starting at 1)
  • Always Be Prepared 1 $stmt = $dbh -> prepare ( 'SELECT * FROM mytable WHERE id = :id' ); 2 $stmt -> bindParam ( 'id' , $_GET [ 'id' ], PDO::PARAM_INT); 3 //$stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT); 4 $stmt -> execute (); 5 $result = $stmt -> fetchAll ();
  • Reading Results
    • PDO allows different ways to fetch data
      • PDO::FETCH_ASSOC
      • PDO::FETCH_NUM
      • PDO::FETCH_BOTH
      • PDO::FETCH_OBJ
      • PDO::FETCH_CLASS
  • First 3 are Familiar 1 // Fetching a single row from the DB 2 $stmt = $dbh -> prepare ( 'SELECT * FROM mytable WHERE id = :id' ); 3 $stmt -> bindParam ( 'id' , $_GET [ 'id' ], PDO::PARAM_INT); 4 $result = $stmt -> fetch (PDO::FETCH_ASSOC); 5 6 foreach ( $result as $key => $val ) { 7 echo $key . ': ' . $val . '<br>' ; 8 } 9 10 // id: 1 11 // value: myname
  • First 3 are Familiar 1 // Fetching a single row from the DB 2 $stmt = $dbh -> prepare ( 'SELECT * FROM mytable WHERE id = :id' ); 3 $stmt -> bindParam ( 'id' , $_GET [ 'id' ], PDO::PARAM_INT); 4 $result = $stmt -> fetch (PDO::FETCH_NUM); 5 6 foreach ( $result as $key => $val ) { 7 echo $key . ': ' . $val . '<br>' ; 8 } 9 10 // 0: 1 11 // 1: myname
  • First 3 are Familiar 1 // Fetching a single row from the DB 2 $stmt = $dbh -> prepare ( 'SELECT * FROM mytable WHERE id = :id' ); 3 $stmt -> bindParam ( 'id' , $_GET [ 'id' ], PDO::PARAM_INT); 4 $result = $stmt -> fetch (PDO::FETCH_BOTH); 5 6 foreach ( $result as $key => $val ) { 7 echo $key . ': ' . $val . '<br>' ; 8 } 9 10 // id: 1 11 // 0: 1 12 // value: myname 13 // 1: myname
  • PDO::FETCH_OBJ Returns an Object 1 // Fetching a single row from the DB 2 $stmt = $dbh -> prepare ( 'SELECT * FROM mytable WHERE id = :id' ); 3 $stmt -> bindParam ( 'id' , $_GET [ 'id' ], PDO::PARAM_INT); 4 $result = $stmt -> fetch (PDO::FETCH_OBJ); 5 6 var_dump ( $result ); 7 8 //object(stdClass)#1 (2) { 9 // [&quot;id&quot;]=> 10 // int(1) 11 // [&quot;value&quot;]=> 12 // string(6) &quot;myname&quot; 13 //}
  • PDO::FETCH_CLASS is Neat
    • Takes a result and turns it into a class
    • Use to instatiate a class directly from the DB
  • Our Class 1 class MyClass { 2 protected $id ; 3 protected $value ; 4 5 public function __set ( $key , $val ) { 6 $function = 'set' . ucfirst ( $key ); 7 $this -> $function ( $val ); 8 } 9 10 public function setId ( $val ) { 11 $this ->id = $id ; 12 } 13 14 public function setValue ( $val ) { 15 $this ->value = 'Value: ' . $val ; 16 } 17 18 public function getId () { return $this ->id; } 19 20 public function getValue () { return $this ->value; } 21 }
  • PDO -> Class 1 // Fetching all rows from the DB 2 $stmt = $dbh -> prepare ( 'SELECT * FROM mytable' ); 3 $result = $stmt -> fetchAll (PDO::FETCH_CLASS, 'MyClass' ); 4 5 foreach ( $result as $class ) { 6 echo $class -> getId (). ': ' . $class -> getValue (); 7 } 8 9 // 1: Value: myname 10 // 2: Value: othername
  • The Shoulders of Giants
    • They know more than we do (sometimes)
    • They take care of bugs
    • They did most of the work
    • Why reinvent the wheel?
  • Zend_Db
    • Default database accessor for Zend Framework
    • Allows for Row Data Gateway access
    • Couple with Zend_Db_Table to allow for Table Data Gateway
    • Zend_Db_Select for statement building
    • Easy to pick up and learn
    • Lots of magic to save you time
  • Easier to Define Connections
    • Uses PDO or native functions
    • Same layout for all connections
    1 $db = Zend_Db:: factory ( 'Pdo_Mysql' , array ( 2 'host' => 'localhost' , 3 'username' => 'root' , 4 'password' => '' , 5 'dbname' => 'mydb' , 6 );
  • Statements
    • Can be direct queries with binding:
    1 $stmt = $db -> query ( 2 'SELECT * FROM mytable WHERE id = ?' , 3 array ( $_GET [ 'id' ));
    • Can be built via PHP:
    1 $select = $db -> select ()-> from ( 'mytable' ) 2 -> where ( 'id = ?' , $_GET [ 'id' ]);
  • Multiple Fetching Options
    • Can fetch everything with ->fetchAll()
    • Can fetch a single row with ->fetch()
    • Can use different modes (NUM, ASSOC, OBJ)
  • Zend_Db_Table
    • Makes working with tables easier
    1 class MyTable extends Zend_Db_Table_Abstract { 2 protected $_name = 'mytable' ; 3 } 4 5 // Create Instance 6 $table = new MyTable (); 7 $table -> insert ( array ( 'value' => 'New Value' )); 8 9 // Update the table 10 $where = $table -> getAdapter ()-> quoteInto ( 'id = ?' , 1 ); 11 $table -> update ( array ( 'value' => 'Newer Value' ), $where ); 12 13 // Delete the row 14 $table -> delete ( $where ); 15 16 // Select something 17 $select = $table -> select ()-> where ( 'id < 10' ); 18 $result = $table -> fetchAll ( $select );
  • PhpORM
    • Halfway between Zend_Db and Doctrine
    • Sits on top of Zend_Db (soon directly PDO)
    • Allows for 1:n or 1:1 relationships between entities
    • Some basic CLI commands
    • Decouples Objects from the Database
  • Basic Ideas
    • Doesn't matter where the data comes from
    • Entities should be defined by business requirements, not the database schema
    • Make building apps easy without extra 'stuff'
  • Entities
    • Objects your app needs to use
    1 Class Animal extends PhpORM_Entity 2 { 3 protected $_daoObjectName = 'Dao_Animals' ; 4 5 /** type=primary_autoincrement **/ 6 protected $id ; 7 /** type=varchar length=50 **/ 8 protected $type ; 9 /** type=date **/ 10 protected $inductionDate ; 11 /** type=varchar length=50 null **/ 12 protected $name ; 13 }
  • Repositories Fetch Entities 1 class Repository_Animals extends PhpORM_Repository 2 { 3 protected $_daoObjectName = 'Dao_Animals' ; 4 protected $_entityName = 'Animal' ; 5 } 6 7 $repo = new Repository_Animals (); 8 // Return all the cats 9 $cats = $repo -> fetchAllBy ( 'type' , 'cat' ); 10 // Find the specified object by primary key 11 $dog = $repo -> find ( $dogId ); 12 // Search by something other than primary key 13 $fluffy = $repo -> findOneBy ( 'name' , 'Fluffy' );
  • Doctrine
    • Hugely popular ORM solution
    • Doctrine 2 is a huge rewrite and only PHP 5.3+
      • I'll be showing 1.2
    • Allows for complex relationships
    • Can generate migration models
    • Models are generated from config files
  • Rich Command Line Client
    • Client takes care of creating PHP code for you
    • Can either go from Yaml -> PHP + DB
    • Or you can go from Db -> PHP Objects
  • Yaml Files Generate Objects 1 Project: 2 columns: 3 id: 4 type: integer 5 primary: true 6 autoincrement: true 7 name: string ( 255 ) 8 description: text 1 $project = new Project (); 2 $project ->id = 4 ; 3 $project ->name = 'My Project' ; 4 $project ->description = 'A description' ; 5 $project -> save ();
  • Doctrine has way more
    • DQL – Doctrine Query Language
    • Lazy Loading of relationships
    • Caching
  • Links
    • PDO
      • http://php.net/manual/en/book.pdo.php
    • Zend_Db
      • http://framework.zend.com/manual/en/zend.db.html
    • PhpORM
      • https://github.com/dragonmantank/PhpORM
    • Doctrine
      • http://www.doctrine-project.org/
  • Thanks!