Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2008, 04:58 AM
Junior Member
 
Join Date: Apr 2008
Posts: 10
Default Zend DB usage confusinon,use model or sql query?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-10-2008, 07:02 PM
Junior Member
 
Join Date: Mar 2008
Posts: 11
Default

I enjoy using the model approach. I feel like it is more structured, modular code. After all, isn't that why we use the Zend Framework's OOP practices?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-10-2008, 07:10 PM
Senior Member
 
Join Date: Jun 2008
Location: Florida
Posts: 108
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 07:46 PM
Junior Member
 
Join Date: Jul 2008
Posts: 5
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-02-2008, 04:49 AM
Junior Member
 
Join Date: Apr 2008
Posts: 10
Default Thanks all for you help and advice

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:
Originally Posted by StGeneral View Post
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 08:45 PM.