I'm writing a small application whose underlying layout is by and large
based on Matthew's pastebin. So, the front controller has an initializer
class plugged in, which initializes the database connection, among other
things.

Now, I want the method -- or a similar method, if I'm mixing apples and
oranges here -- to initialize the tables on the database first if it's the
first time it's running. (I'm working with Sqlite so all else is handled by
the server itself.)

The following little hack of the original initDb method does the job.
Basically, I threw in a cache check to see if the tables already exist and,
in case they don't, we go ahead and create them, in tedious procedural
style.

public function initDb()
>
{
>
$config = $this->_config->db;
>
$db = Zend_Db::factory($config->cxn);
>

> Zend_Db_Table_Abstract::setDefaultAdapter($db);
>

> if (!($this->_cache->load('tables_exist'))) {
>

> $sql = array();
>
$sql[] = 'CREATE TABLE IF NOT EXISTS foo (id INTEGER PRIMARY KEY
> AUTOINCREMENT, some_text TEXT)';
>
$sql[] = 'CREATE TABLE IF NOT EXISTS bar (id INTEGER PRIMARY KEY
> AUTOINCREMENT, some_text TEXT)';
>
foreach ($sql as $q) {
>
$db->query($q);
>
}
>

> $this->_cache->save(1, null, array(), null);
>

> }
>

> return $this;
>
}
>

I'm wondering if there's a more beautiful way to accomplish this, perhaps by
moving table definitions to a config. Would love to hear your comments.
Cheers!

Senol

-- 

~+ http://ultra.bohe.me +~

Reply via email to