2. What we've learned and what we do 3. Oooh!Let's create the schema! 4. Write code that uses the DB 5.
6. ActiveRecord 7. Table/Row Data Gateway 8. Plain Old Mysql (POM) 9. And then 10.
11. we end up refactoring for every new feature(esp. caching, logging) 12. we need to go to a Service Oriented Architecture,and refactor twice 13. STOP THE MADNESS! 14. Step One 15. Models are just classes. Create classes. 16. < ?php class Person { protected $_email ; protected $_password ; protected $_username ; public function __get ( $name ) { $local = '_'. $name ; if ( property_exists ( $this , $local )) { return $this -> $local ; } return null ; } public function __set ( $name , $value ) { $local = '_'. $name ; if (! property_exists ( $this , $local )) { throw new InvalidArgumentException (); } $this -> $local = $value ; } } 17. Step Two 18.
19. Identify how you'll persist the data 20. Write code for persisting data(Data Access Layer) 21. CREATE TABLE person ( username VARCHAR PRIMARY KEY , password VARCHAR , email VARCHAR , ); < ?php class PersonTable extends Zend_Db_Table_Abstract { protected $_name = 'person' ; protected $_primary = 'username' ; } 22. Step Three 23.
24. Transaction Script 25. < ?php class PersonMapper { public function save ( Person $person ) { $data = array ( 'username' => $person -> username , 'password' => $person -> password , 'email' => $person -> email , ); $this -> getTable ()-> save ( $data ); } public function fetch ( $username ); public function getTable (); public function setTable ( $table ); } 26. Step Four 27.
28. Allows easy re-use of your application via services 29. Write CLI scripts that consume the Service Layer 30.
31. Authentication and Authorization 32. Transactions and interactions between models 33. < ?php class PersonService { public function create ( array $data ) { $person = new Person (); if (! $data = $this -> getValidator ()-> isValid ( $data )) { throw new InvalidArgumentException (); } $person -> username = $data [ 'username' ]; $person -> password = $data [ 'password' ]; $person -> email = $data [ 'email' ]; $this -> getMapper ()-> save ( $person ); return $person ; } } 34. Decorating for Fun and Profit 35. Refactor to add caching? No! Decorate! 36. < ?php class CachingPersonMapper { protected $_mapper ; public function __construct ( PersonMapper $mapper ) { $this -> _mapper = $mapper ; } public function fetch ( $username ) { $cache = $this -> getCache (); if (! $person = $cache -> load ( $username )) { $person = $this -> _mapper -> fetch ( $username ); $cache -> save ( $person , $username ); } return $person ; } } 37. Refactor or extendto change the return value (e.g., JSON vs XML vs ...)? No! Decorate! 38. < ?php class JsonPerson { protected $_person ; public function __construct ( Person $person ) { $this -> _person = $person ; } public function __toString () { $data = array ( 'username' => $this -> _person -> username , 'email' => $this -> _person -> email , ); return json_encode ( $data ); } } 39. Congratulations! 40.
41. Good OOP and encapsulation CAN make your life easier 42. Testing is easier than debugging 43. Think beyond the DB! 44. Thank you.