Zend_Db


          2010 / 03 / 06
•   Zend_Framework

•    Zend_Db

•
Zend_Framework

2008   6     1



Zend_Framework              Ver1.5



                 8    Zend_Framework
             ...
Zend_Db

Zend_Db

    Zend_Db_Adapter                   DB

    Zend_Db_Table                    DB
     a) Zend_Db_Table ...
Zend_Db

Zend_Db_Adapter                                 DB
//
$params = array(
    'host'           => 'localhost',
    '...
Zend_Db

Zend_Db_Table                                         DB
a)
     // DAO
     class BookDAO extends App_Db_Table {...
Zend_Db

Zend_Db_Table                                              DB
b)
  // Table
  class BookTable extends App_Db_Tabl...
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.                              ...
Zend_Db

Table
    // Table
    class BookTable extends App_Db_Table {
        protected $_name = 'book';
        protecte...
Zend_Db

Row
                                                  Row                          Rowset


                  Use...
Zend_Db

Rowset


  // Rowset
  class BookRowset extends App_Db_Table_Rowset {
      public function toArrayGroupByAuthor(...
Zf Zend Db by aida
Upcoming SlideShare
Loading in …5
×

Zf Zend Db by aida

1,866 views
1,785 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,866
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide















  • Zf Zend Db by aida

    1. 1. Zend_Db 2010 / 03 / 06
    2. 2. • Zend_Framework • Zend_Db •
    3. 3. Zend_Framework 2008 6 1 Zend_Framework Ver1.5 8 Zend_Framework AX_Framework1.0 6 AX_Framework
    4. 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. 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. 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. 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();
    8. 8. Zend_Db Zend_Db_Table DB b)
    9. 9. Zend_Db Table getXXX getXXX Rowset Array select Row Rowset
    10. 10. Zend_Db
    11. 11. Zend_Db Table 1. select Zend_Db_Table_Select 2. createRow() 3. static
    12. 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. 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. 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();

    ×