SlideShare una empresa de Scribd logo
1 de 15
Zend_Db


          2010 / 03 / 06
•   Zend_Framework

•    Zend_Db

•
Zend_Framework

2008   6     1



Zend_Framework              Ver1.5



                 8    Zend_Framework
              AX_Framework1.0

                                6
       AX_Framework
Zend_Db

Zend_Db

    Zend_Db_Adapter                   DB

    Zend_Db_Table                    DB
     a) Zend_Db_Table                              DAO
                        select   insert   update



     b) Zend_Db_Table Zend_Db_Table_Row Zend_Db_Tabl
          e_Rowset
Zend_Db

Zend_Db_Adapter                                 DB
//
$params = array(
    'host'           => 'localhost',
    'username'       => 'webuser',
    'password'       => 'xxxxxxxx',
    'dbname'         => 'test'
);
$db = Zend_Db::factory('Pdo_Mysql', $params);

//
$results = $db->fetchAll('SELECT * FROM book WHERE id = ?', 123);

//
$data = array(
    'created' => time(),
    'tite' => '               1   ',
     'author' => '   '
);
$db->insert('book', $data);
Zend_Db

Zend_Db_Table                                         DB
a)
     // DAO
     class BookDAO extends App_Db_Table {
         protected $_name = 'book';

          public function getBooksByAuthor($author) {...}

          public function insert($data) {...}
     }

     // DAO
     $dao = new BookDAO();

     //
     $rowset = $dao->fetchAll();
     $rowset = $dao->getBooksByAuthor('         ');


     //
     $data = array('tite' => '                   ','author' => '   ');
     $dao->insert($data);
Zend_Db

Zend_Db_Table                                              DB
b)
  // Table
  class BookTable extends App_Db_Table {
      protected $_name = 'book';
      protected $_rowClass = 'BookRow';
      protected $_rowsetClass = 'BookRowset';

       public function getBooksByAuthor($author) {     }
  }

  // Row
  class BookRow extends App_Db_Table_Row {
  }

  // Rowset
  class BookRowset extends App_Db_Table_Rowset {
  }

  // Table
  $table = new BookTable();

  //
  $rowset = $table->fetchAll();
  $rowset = $table->getBooksByAuthor('        ');


  //
  $data = array('tite' => '                  ', 'author' => '   ');
  $row = $table->createRow($data);
  $row->save();
Zend_Db




Zend_Db_Table   DB   b)
Zend_Db




Table
  getXXX getXXX
                      Rowset   Array
             select

Row


Rowset
Zend_Db
Zend_Db

Table



   1.               select   Zend_Db_Table_Select

   2. createRow()
   3.                               static
Zend_Db

Table
    // Table
    class BookTable extends App_Db_Table {
        protected $_name = 'book';
        protected $_rowClass = 'BookRow';
        protected $_rowsetClass = 'BookRowset';

         public function selectActive() {
             return parent::select('deleted IS NULL');
         }

         public function selectByAuthor($author) {
             return $this->selectActive()->where('author = ?', $author);
         }
    }

    // Table
    $table = new BookTable();

    //
    $rowset = $table->fetchAll($table->selectByAuthor('       '));
    $rowset = $table->fetchAll($table->selectByAuthor('       ')->where('created < ', '1992-03-31'));




  Select
  →DRY
  Zend_Db
Zend_Db

Row
                                                  Row                          Rowset


                  User <                 > Blog <           > Comment
  // Row
  class BlogRow extends App_Db_Table_Row {
      public function getUser() {
          $userTable = new UserTable();
          return $userTable->find($this->user_id)->current();
      }

      public function getComments() {
          $commentTable = new CommentTable();
          return $commentTable->fetchAll($commentTable->select()->where('blog_id = ?', $this->id));
      }
  }

  // Table
  $table = new BlogTable();

  // BlogRow
  $row = $table->find(123)->current();

  // Blog
  $nickname = $row->getUser()->nickname;

  // Blog
  $comments = $row->getComments();
Zend_Db

Rowset


  // Rowset
  class BookRowset extends App_Db_Table_Rowset {
      public function toArrayGroupByAuthor() {
          $array = array();
          while ($this->valid()) {
              $row = $this->current();
              $array[$row->author] = isset($array[$row->author]) ? array_push($array[$row->author], $row) : array($row);
              $this->next();
          }
      }
  }

  // Table
  $table = new BookTable();

  // Author
  $authorsArray = $table->fetchAll()->toArrayGroupByAuthor();
Zf Zend Db by aida

Más contenido relacionado

La actualidad más candente

The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of LithiumNate Abele
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHPmarkstory
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate Abele
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionNate Abele
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Editionddiers
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Hiroshi Shibamura
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of LithiumNate Abele
 
15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilorRazvan Raducanu, PhD
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleHugo Hamon
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201Fabien Potencier
 
Php update and delet operation
Php update and delet operationPhp update and delet operation
Php update and delet operationsyeda zoya mehdi
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConRafael Dohms
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles Anis Ahmad
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介Jace Ju
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkG Woo
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 

La actualidad más candente (20)

The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor15. CodeIgniter editarea inregistrarilor
15. CodeIgniter editarea inregistrarilor
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
 
Php update and delet operation
Php update and delet operationPhp update and delet operation
Php update and delet operation
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Revisiting SOLID Principles
Revisiting  SOLID Principles Revisiting  SOLID Principles
Revisiting SOLID Principles
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 

Destacado

General content customer presentation amplify teams
General content customer presentation amplify teamsGeneral content customer presentation amplify teams
General content customer presentation amplify teamsXavier Baez Gutierrez
 
Integração Contínua com CruiseControl e phpUnderControl
Integração Contínua com CruiseControl e phpUnderControlIntegração Contínua com CruiseControl e phpUnderControl
Integração Contínua com CruiseControl e phpUnderControlDiego Tremper
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionIn a Rocket
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting PersonalKirsty Hulse
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanPost Planner
 

Destacado (6)

Zend Framework
Zend FrameworkZend Framework
Zend Framework
 
General content customer presentation amplify teams
General content customer presentation amplify teamsGeneral content customer presentation amplify teams
General content customer presentation amplify teams
 
Integração Contínua com CruiseControl e phpUnderControl
Integração Contínua com CruiseControl e phpUnderControlIntegração Contínua com CruiseControl e phpUnderControl
Integração Contínua com CruiseControl e phpUnderControl
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
 

Similar a Zf Zend Db by aida

Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010leo lapworth
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7chuvainc
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Leonardo Proietti
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
CodeIgniter Class Reference
CodeIgniter Class ReferenceCodeIgniter Class Reference
CodeIgniter Class ReferenceJamshid Hashimi
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friendkikoalonsob
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2zfconfua
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injectionJosh Adell
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011Alessandro Nadalin
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperJonathan Wage
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?Alexandru Badiu
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of TransductionDavid Stockton
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018Adam Tomat
 

Similar a Zf Zend Db by aida (20)

Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
DBI
DBIDBI
DBI
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
CodeIgniter Class Reference
CodeIgniter Class ReferenceCodeIgniter Class Reference
CodeIgniter Class Reference
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friend
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
PHP API
PHP APIPHP API
PHP API
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injection
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
Doctrine and NoSQL
Doctrine and NoSQLDoctrine and NoSQL
Doctrine and NoSQL
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 
Doctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document MapperDoctrine MongoDB Object Document Mapper
Doctrine MongoDB Object Document Mapper
 
What's new in the Drupal 7 API?
What's new in the Drupal 7 API?What's new in the Drupal 7 API?
What's new in the Drupal 7 API?
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 

Zf Zend Db by aida

  • 1. Zend_Db 2010 / 03 / 06
  • 2. Zend_Framework • Zend_Db •
  • 3. Zend_Framework 2008 6 1 Zend_Framework Ver1.5 8 Zend_Framework AX_Framework1.0 6 AX_Framework
  • 4. Zend_Db Zend_Db Zend_Db_Adapter DB Zend_Db_Table DB a) Zend_Db_Table DAO select insert update b) Zend_Db_Table Zend_Db_Table_Row Zend_Db_Tabl e_Rowset
  • 5. Zend_Db Zend_Db_Adapter DB // $params = array( 'host' => 'localhost', 'username' => 'webuser', 'password' => 'xxxxxxxx', 'dbname' => 'test' ); $db = Zend_Db::factory('Pdo_Mysql', $params); // $results = $db->fetchAll('SELECT * FROM book WHERE id = ?', 123); // $data = array( 'created' => time(), 'tite' => ' 1 ', 'author' => ' ' ); $db->insert('book', $data);
  • 6. Zend_Db Zend_Db_Table DB a) // DAO class BookDAO extends App_Db_Table { protected $_name = 'book'; public function getBooksByAuthor($author) {...} public function insert($data) {...} } // DAO $dao = new BookDAO(); // $rowset = $dao->fetchAll(); $rowset = $dao->getBooksByAuthor(' '); // $data = array('tite' => ' ','author' => ' '); $dao->insert($data);
  • 7. Zend_Db Zend_Db_Table DB b) // Table class BookTable extends App_Db_Table { protected $_name = 'book'; protected $_rowClass = 'BookRow'; protected $_rowsetClass = 'BookRowset'; public function getBooksByAuthor($author) { } } // Row class BookRow extends App_Db_Table_Row { } // Rowset class BookRowset extends App_Db_Table_Rowset { } // Table $table = new BookTable(); // $rowset = $table->fetchAll(); $rowset = $table->getBooksByAuthor(' '); // $data = array('tite' => ' ', 'author' => ' '); $row = $table->createRow($data); $row->save();
  • 9. Zend_Db Table getXXX getXXX Rowset Array select Row Rowset
  • 11. Zend_Db Table 1. select Zend_Db_Table_Select 2. createRow() 3. static
  • 12. Zend_Db Table // Table class BookTable extends App_Db_Table { protected $_name = 'book'; protected $_rowClass = 'BookRow'; protected $_rowsetClass = 'BookRowset'; public function selectActive() { return parent::select('deleted IS NULL'); } public function selectByAuthor($author) { return $this->selectActive()->where('author = ?', $author); } } // Table $table = new BookTable(); // $rowset = $table->fetchAll($table->selectByAuthor(' ')); $rowset = $table->fetchAll($table->selectByAuthor(' ')->where('created < ', '1992-03-31')); Select →DRY Zend_Db
  • 13. Zend_Db Row Row Rowset User < > Blog < > Comment // Row class BlogRow extends App_Db_Table_Row { public function getUser() { $userTable = new UserTable(); return $userTable->find($this->user_id)->current(); } public function getComments() { $commentTable = new CommentTable(); return $commentTable->fetchAll($commentTable->select()->where('blog_id = ?', $this->id)); } } // Table $table = new BlogTable(); // BlogRow $row = $table->find(123)->current(); // Blog $nickname = $row->getUser()->nickname; // Blog $comments = $row->getComments();
  • 14. Zend_Db Rowset // Rowset class BookRowset extends App_Db_Table_Rowset { public function toArrayGroupByAuthor() { $array = array(); while ($this->valid()) { $row = $this->current(); $array[$row->author] = isset($array[$row->author]) ? array_push($array[$row->author], $row) : array($row); $this->next(); } } } // Table $table = new BookTable(); // Author $authorsArray = $table->fetchAll()->toArrayGroupByAuthor();

Notas del editor