Omeka_Db

Package: Db

class Omeka_Db

Database manager object for Omeka

While mostly a wrapper for a Zend_Db_Adapter instance, this also provides shortcuts for retrieving table objects and table names for use in SQL.

property prefix

string|null

The prefix that every table in the omeka database will use.

property _adapter

protected Zend_Db_Adapter_Abstract

The database adapter.

property _tables

protected array

All the tables that are currently managed by this database object.

__construct($adapter, $prefix = null)
Parameters:
  • $adapter (Zend_Db_Adapter_Abstract) – A Zend Framework connection object.
  • $prefix (string) – The prefix for the database tables, if applicable.
__call($m, $a)

Delegate to the database adapter.

Parameters:
  • $m (string) – Method name.
  • $a (array) – Method arguments.
Returns:

mixed

__get($name)

Magic getter is a synonym for Omeka_Db::getTableName().

Example: $db->Item is equivalent to $db->getTableName(‘Item’).

Parameters:
  • $name (string) – Property name; table model class name in this case.
Returns:

string|null

setLogger($logger)

Set logger for SQL queries.

Parameters:
  • $logger (Zend_Log) –
getAdapter()

Retrieve the database adapter.

Returns:Zend_Db_Adapter_Abstract
getTableName($class)

Retrieve the name of the table (including the prefix).

Parameters:
  • $class
Returns:

string

hasPrefix()

Check whether the database tables have a prefix.

Returns:bool
getTable($class)

Retrieve a table object corresponding to the model class.

Table classes can be extended by inheriting off of Omeka_Db_Table and then calling your table Table_ModelName, e.g. Table_Item or Table_Collection. For backwards compatibility you may call your table ModelNameTable, i.e. ItemTable or CollectionTable. The latter naming pattern is deprecated.

This will cache every table object so that tables are not instantiated multiple times for complicated web requests.

Parameters:
  • $class (string) – Model class name.
Returns:

Omeka_Db_Table

setTable($alias, Omeka_Db_Table $table)

Cache a table object.

Prevents the creation of unnecessary instances.

Parameters:
insert($table, $values = array())

Every query ends up looking like: INSERT INTO table (field, field2, field3, …) VALUES (?, ?, ?, …) ON DUPLICATE KEY UPDATE field = ?, field2 = ?, …

Note on portability: ON DUPLICATE KEY UPDATE is a MySQL extension. The advantage to using this is that it doesn’t care whether a row exists already. Basically it combines what would be insert() and update() methods in other ORMs into a single method

Parameters:
  • $table (string) – Table model class name.
  • $values (array) – Rows to insert (or update).
Returns:

int The ID for the row that got inserted (or updated).

log($sql)

Log SQL query if logging is configured.

This logs the query before variable substitution from bind params.

Parameters:
  • $sql (string|Zend_Db_Select) –
queryBlock($sql, $delimiter = ';')

Execute more than one SQL query at once.

Parameters:
  • $sql (string) – String containing SQL queries.
  • $delimiter (string) – Character that delimits each SQL query.
loadSqlFile($filePath)

Read the contents of an SQL file and execute all the queries therein.

In addition to reading the file, this will make substitutions based on specific naming conventions. Currently makes the following substitutions: %PREFIX% will be replaced by the table prefix.

Parameters:
  • $filePath (string) – Path to the SQL file to load