SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
Hiroshi Shibamura
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
Fabien Potencier
 
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
Fabien Potencier
 

Was ist angesagt? (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
 

Andere mochten auch (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
 

Ähnlich wie Zf Zend Db by aida

DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friend
kikoalonsob
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 

Ähnlich wie 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();

Hinweis der Redaktion