Jump to content

Zend Framework: Zend_Db Best Practices


BrandonK

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.