Escolar Documentos
Profissional Documentos
Cultura Documentos
Bill Karwin
Introduction
Whats in the Zend_Db component? Examples of using each class Using Zend_Db in MVC applications
2007-10-09 | Page 2
2007-10-09 | Page 3
2007-10-09 | Page 4
2007-10-09 | Page 5
2007-10-09 | Page 6
2007-10-09 | Page 7
2007-10-09 | Page 8
2007-10-09 | Page 9
2007-10-09 | Page 10
2007-10-09 | Page 11
2007-10-09 | Page 12
2007-10-09 | Page 13
Database Adapters
2007-10-09 | Page 14
Connecting to a database
Use the static factory() method of Zend_Db:
require_once Zend/Db.php; $db = Zend_Db::factory(Mysqli, array( host => localhost, dbname => test, username=> webappuser, password=> xxxxxxxx) );
2007-10-09 | Page 15
2007-10-09 | Page 16
You can fetch rows one-by-one in a loop You can fetch rows as other structures
2007-10-09 | Page 17
2007-10-09 | Page 18
Inserting data
Use the insert() method. Pass the table name, and an associative array
mapping columns to values:
$db->insert( bugs, array( bug_description => help me, bug_status => NEW) );
2007-10-09 | Page 19
Updating data
Use the update() method. Pass the table name, an associative array
mapping columns to new values, and an expression for the WHERE clause:
$n = $db->update( bugs, array(bug_status => NEW), bug_id = 123 );
2007-10-09 | Page 20
Deleting data
Use the delete() method. Pass the table name, and
an expression for the WHERE clause:
$n = $db->delete(bugs, bug_id = 321);
2007-10-09 | Page 21
2007-10-09 | Page 22
2007-10-09 | Page 23
Quoting SQL
2007-10-09 | Page 24
2007-10-09 | Page 25
2007-10-09 | Page 26
2007-10-09 | Page 27
2007-10-09 | Page 28
Query Profiler
2007-10-09 | Page 29
2007-10-09 | Page 30
2007-10-09 | Page 32
Query Builder
2007-10-09 | Page 33
2007-10-09 | Page 34
You can mix fluent and traditional usage. You can add SQL clauses in any order.
$select = $db->select()->from(bugs)->order(bug_id); if ($condition == true) { $select->joinNatural(other_table); }
2007-10-09 | Page 35
$select = $db->select() ->distinct() ->from(array(a=>accounts), array(account_name, num_bugs=>COUNT(*))) ->joinLeft(array(b=>bugs), b.reported_by = a.account_name) ->group(a.account_name) ->having(num_bugs < 5) ->order(a.account_name ASC) ->limit(10, 20);
2007-10-09 | Page 36
2007-10-09 | Page 37
2007-10-09 | Page 38
2007-10-09 | Page 39
2007-10-09 | Page 40
A Rowset object is iterable and countable. A Rowset is a collection of Row objects. A Row has an accessor for each column.
foreach ($rowset as $row) { echo $row->bug_id: $row->bug_description\n; }
2007-10-09 | Page 41
2007-10-09 | Page 42
2007-10-09 | Page 43
2007-10-09 | Page 44
2007-10-09 | Page 45
2007-10-09 | Page 46
2007-10-09 | Page 47
2007-10-09 | Page 49
2007-10-09 | Page 50
2007-10-09 | Page 51
2007-10-09 | Page 52
2007-10-09 | Page 53
Recap of Zend_Db Adapters to PHP database extensions Quoting SQL identifiers and values Query profiler SELECT query builder Table Data Gateway &
Row Data Gateway
2007-10-09 | Page 54
Thanks
http://framework.zend.com/