Author Topic: Zend Framework: Zend_Db Best Practices  (Read 3162 times)

0 Members and 1 Guest are viewing this topic.

Offline BrandonKTopic starter

  • Irregular
  • Posts: 31
    • View Profile
Zend Framework: Zend_Db Best Practices
« on: March 11, 2008, 02:10:00 PM »
I plan on using the Zend_Framework in my next big project, and I am starting to play around with it.  I am currently debating Zend_Db vs the standard PDO and I think that the ZF version is a lot nicer.  I plan to extend the class just a little and wrap it in my own namespace.  I have created the connection like this:
Code: [Select]
<?php
try {

$db_config = array(
'host'     => $hostname,
'username' => $username,
'password' => $password,
'dbname'   => $dbname,
'profiler' => true,
'adapterNamespace' => 'My_Db_Adapter'
); //in the future I may use load from Zend_Config or something
$db Zend_Db::factory('Pdo_Mysql'$db_config);
} catch (Exception $e) {
throw $e;
}
This works fine, and I think its fairly straight forward.  One problem I have is related to the exceptions, but that's a different post I guess.

Then I want to come down and make some queries:
Code: [Select]
<?php
$sql = "
SELECT name
     , alpha
  FROM countries
 WHERE id < :max_id
   AND alpha LIKE :alpha"
;

$stmt $db->prepare($sql);

$stmt->bindValue(':max_id'5PDO::PARAM_INT);
$stmt->bindValue(':alpha''A%');
$stmt->execute();
But this is where I get mixed up as to what I should be doing.  Zend_Db::execute() will return false when the query has an error (right?), but it will also throw an exception.  Exceptions override a return, so I have to put a try {} catch() {} around every query I make??  Without implementing a Table Data Gateway or other data access pattern, is there a "better" way to use this class?