Archive for April, 2008

Zend Layout and Navigation

Tuesday, April 1st, 2008

I’m not sure how everyone else is using Zend_Layout, but I’m doing the following to render my navigation and footer links. It’s not the most DRY code, but works until I learn more about the framework.

In my Action file, I add actions to the stack for nav, footer, (more…)

Zend Framework 1.5.1 and my Models code

Tuesday, April 1st, 2008

Someone asked how I get a db connection in my models using ZF1.5.1

Basically I have a ConfigDB singleton pattern that gets one db connection, then sets the default adapter so when I extend the Zend_DB_Table_Abstract class, the default connection gets carried around.

class ConfigDB {
static private $instance;
private function __construct() {
}
public static function getInstance() {
if (!ConfigDB::$instance) {
$params = array(
/* redacted */
);
ConfigDB::$instance = Zend_Db::factory('Mysqli', $params);
Zend_Db_Table_Abstract::setDefaultAdapter(ConfigDB::$instance);
}
return ConfigDB::$instance;
}
public static function selectDb($db) {
return ConfigDB::$instance->getConnection()->select_db($db);
}
}
ConfigDB::getInstance();

That grabs the connection and sets the default adapter. So in my model class I can do:
class Country extends Zend_Db_Table_Abstract {
protected $_name = 'countries';
protected $_primary = 'country_id';
function __construct($country = 1, $abbr = '')
{
parent::__construct();
}
}

That’s it