Jump to content

Archived

This topic is now archived and is closed to further replies.

BrandonK

Zend Framework: Zend_Db Best Practices

Recommended Posts

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:

<?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:

<?php
$sql =	"
SELECT name
     , alpha
  FROM countries
WHERE id < :max_id
   AND alpha LIKE :alpha";

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

$stmt->bindValue(':max_id', 5, PDO::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?

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.