|
|||
|
Hi all I am confused,
Which is the better way to use zend db, is it by writing pure own sql queries And use in select statements)as in PART A below) or use models of tables and join with Given model relationships (PART B below) ------------------------------------------------------------------ Example PART A ------------------------------------------------------------------ $select*=*$db->select() ****->from(array('p'*=>*'products'), ********array('product_id')) ****->join(array('l'*=>*'line_items'), ********'p.product_id*=*l.product_id', ********array('line_items_per_product'*=>*'COUNT(* )')) ****->group('p.product_id') ****->having('line_items_per_product*>*10'); ------------------------------------------------------------------ <?php $db*=*Zend_Db::factory(*...options...*); $select*=*$db->select(); ------------------------------------------------------------------ $sql*=*'SELECT***FROM*bugs*WHERE*bug_id*=*?'; $result*=*$db->fetchAll($sql,*2); ------------------------------------------------------------------ //////////////// OR ///////////////////// ------------------------------------------------------------------ Example PART B ------------------------------------------------------------------ class*Products*extends*Zend_Db_Table_Abstract { ****protected*$_name************=*'products'; ****protected*$_dependentTables*=*array('BugsProdu cts'); } class*Bugs*extends*Zend_Db_Table_Abstract { ****protected*$_name************=*'bugs'; ****protected*$_dependentTables*=*array('BugsProdu cts'); ****protected*$_referenceMap****=*array( ********'Reporter'*=>*array( ************'columns'***********=>*'reported_by', ************'refTableClass'*****=>*'Accounts', ************'refColumns'********=>*'account_name' ********), -------------------------------------------------------------------------------- <?php $bugs*=*new*Bugs(); $row*=*$bugs->fetchRow('bug_id*=*1'); ------------------------------------------------------------------ I understand each will have their benefits but which one is preferable Like in joining the table with defining relationships we the data retrieved will be read Only. Please help me, Thank you for your help and time |
|
|||
|
For very simple queries (retrieving arrays to populate select lists) I write out SQL strings. For more complex queries, and for those that need to be built dynamically or used across controllers, I use model classes that extend Zend_Db_Table_Abstract.
|
|
|||
|
If you like OOP I think you'd like to work with DB using objects. Just read about ORM, patterns that were used to build Zend_Db_Table_* classes and the examples of Zend_Db_Table usage. May be it will help you to see their benefits.
As for me, I try to use sql statemensas as less as I can. Only for complex queris and only using Zend_Db_Select object. |
|
|||
|
Thanks all for you help and advice
i have one more question using join tables how to delete,update and insert with the joined models(tables) can we do that? thanks again Quote:
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|