Omeka_Db_Table

Package: Db\Table

class Omeka_Db_Table

Database table classes.

Subclasses attached to models must follow the naming convention: Table_TableName, e.g. Table_ElementSet in models/Table/ElementSet.php.

property _target

protected string

The name of the model for which this table will retrieve objects.

property _name

protected string

The name of the table (sans prefix).

If this is not given, it will be inflected.

property _tablePrefix

protected string

The table prefix.

Generally used to differentiate Omeka installations sharing a database.

property _db

protected Omeka_Db

The Omeka database object.

__construct($targetModel, $db)

Construct the database table object.

Do not instantiate this by itself. Access instances only via Omeka_Db::getTable().

Parameters:
  • $targetModel (string) – Class name of the table’s model.

  • $db (Omeka_Db) – Database object to use for queries.

__call($m, $a)

Delegate to the database adapter.

Used primarily as a convenience method. For example, you can call fetchOne() and fetchAll() directly from this object.

Parameters:
  • $m (string) – Method name.

  • $a (array) – Method arguments.

Returns:

mixed

getTableAlias()

Retrieve the alias for this table (the name without the prefix).

Returns:

string

getDb()

Retrieve the Omeka_Db instance.

Returns:

Omeka_Db

hasColumn($field)

Determine whether a model has a given column.

Parameters:
  • $field (string) – Field name.

Returns:

bool

getColumns()

Retrieve a list of all the columns for a given model.

This should be here and not in the model class because get_class_vars() returns private/protected properties when called from within the class. Will only return public properties when called in this fashion.

Returns:

array

getTableName()

Retrieve the name of the table for the current table (used in SQL statements).

If the table name has not been set, it will inflect the table name.

Returns:

string

setTableName($name = null)

Set the name of the database table accessed by this class.

If no name is provided, it will inflect the table name from the name of the model defined in the constructor. For example, Item -> items.

Parameters:
  • $name (string) – (optional) Table name.

getTablePrefix()

Retrieve the table prefix for this table instance.

Returns:

string

setTablePrefix($tablePrefix = null)

Set the table prefix.

Defaults to the table prefix defined by the Omeka_Db instance. This should remain the default in most cases. However, edge cases may require customization, e.g. creating wrappers for tables generated by other applications.

Parameters:
  • $tablePrefix (string|null) –

find($id)

Retrieve a single record given an ID.

Parameters:
  • $id (int) –

Returns:

Omeka_Record_AbstractRecord|false

findAll()

Get a set of objects corresponding to all the rows in the table

WARNING: This will be memory intensive and is thus not recommended for large data sets.

Returns:

array Array of {@link Omeka_Record_AbstractRecord}s.

findPairsForSelectForm($options = array())

Retrieve an array of key=>value pairs that can be used as options in a <select> form input.

Parameters:
  • $options (array) – (optional) Set of parameters for searching/ filtering results.

Returns:

array

_getColumnPairs()

Retrieve the array of columns that are used by findPairsForSelectForm().

This is a template method because these columns are different for every table, but the underlying logic that retrieves the pairs from the database is the same in every instance.

Returns:

array

findBy($params = array(), $limit = null, $page = null)

Retrieve a set of model objects based on a given number of parameters

Parameters:
  • $params (array) – A set of parameters by which to filter the objects that get returned from the database.

  • $limit (int) – Number of objects to return per “page”.

  • $page (int) – Page to retrieve.

Returns:

array|null The set of objects that is returned

getSelect()

Retrieve a select object for this table.

Returns:

Omeka_Db_Select

getSelectForFindBy($params = array())

Retrieve a select object that has had search filters applied to it.

Parameters:
  • $params (array) – optional Set of named search parameters.

Returns:

Omeka_Db_Select

getSelectForFind($recordId)

Retrieve a select object that is used for retrieving a single record from the database.

Parameters:
  • $recordId (int) –

Returns:

Omeka_Db_Select

applySearchFilters($select, $params)

Apply a set of filters to a Select object based on the parameters given.

By default, this simply checks the params for keys corresponding to database column names. For more complex filtering (e.g., when other tables are involved), or to use keys other than column names, override this method and optionally call this parent method.

Parameters:
applySorting($select, $sortField, $sortDir)

Apply default column-based sorting for a table.

Parameters:
  • $select (Omeka_Db_Select) –

  • $sortField (string) – Field to sort on.

  • $sortDir (string) – Direction to sort.

applyPagination($select, $limit, $page = null)

Apply pagination to a select object via the LIMIT and OFFSET clauses.

Parameters:
  • $select (Zend_Db_Select) –

  • $limit (int) – Number of results per “page”.

  • $page (int|null) – Page to retrieve, first if omitted.

Returns:

Zend_Db_Select

findBySql($sqlWhereClause, $params = array(), $findOne = false)

Retrieve an object or set of objects based on an SQL WHERE predicate.

Parameters:
  • $sqlWhereClause (string) –

  • $params (array) – optional Set of parameters to bind to the WHERE clause. Used to prevent security flaws.

  • $findOne (bool) – optional Whether or not to retrieve a single record or the whole set (retrieve all by default).

Returns:

array|Omeka_Record_AbstractRecord|false

count($params = array())

Retrieve a count of all the rows in the table.

Parameters:
  • $params (array) – optional Set of search filters upon which to base the count.

Returns:

int

exists($id)

Check whether a row exists in the table.

Parameters:
  • $id (int) –

Returns:

bool

filterByPublic(Omeka_Db_Select $select, $isPublic)

Apply a public/not public filter to the select object.

A convenience function than derivative table classes may use while applying search filters.

Parameters:
filterByFeatured(Omeka_Db_Select $select, $isFeatured)

Apply a featured/not featured filter to the select object.

A convenience function than derivative table classes may use while applying search filters.

Parameters:
filterBySince(Omeka_Db_Select $select, $dateSince, $dateField)

Apply a date since filter to the select object.

A convenience function than derivative table classes may use while applying search filters.

Parameters:
  • $select (Omeka_Db_Select) –

  • $dateSince (string) – ISO 8601 formatted date

  • $dateField (string) – “added” or “modified”

filterByUser(Omeka_Db_Select $select, $userId, $userField)

Apply a user filter to the select object.

A convenience function than derivative table classes may use while applying search filters.

Parameters:
filterByRange($select, $range)

Filter returned records by ID.

Can specify a range of valid record IDs or an individual ID

Parameters:
getSelectForCount($params = array())

Retrieve a select object used to retrieve a count of all the table rows.

Parameters:
  • $params (array) – optional Set of search filters.

Returns:

Omeka_Db_Select

checkExists($id)

Check whether a given row exists in the database.

Currently used to verify that a row exists even though the current user may not have permissions to access it.

Parameters:
  • $id (int) – The ID of the row.

Returns:

bool

fetchObjects($sql, $params = array())

Retrieve a set of record objects based on an SQL SELECT statement.

Parameters:
  • $sql (string) – This could be either a string or any object that can be cast to a string (commonly Omeka_Db_Select).

  • $params (array) – Set of parameters to bind to the SQL statement.

Returns:

array|null Set of Omeka_Record_AbstractRecord instances, or null if none can be found.

fetchObject($sql, $params = array())

Retrieve a single record object from the database.

Parameters:
  • $sql (string) –

  • $params (string) – Parameters to substitute into SQL query.

Returns:

Omeka_Record_AbstractRecord or null if no record

recordFromData($data)

Populate a record object with data retrieved from the database.

Parameters:
  • $data (array) – A keyed array representing a row from the database.

Returns:

Omeka_Record_AbstractRecord

_getSortParams($params)

Get and parse sorting parameters to pass to applySorting.

A sorting direction of ‘ASC’ will be used if no direction parameter is passed.

Parameters:
  • $params (array) –

Returns:

array|null Array of sort field, sort dir if params exist, null otherwise.

_getHookName($suffix)

Get the name for a model-specific hook or filter..

Parameters:
  • $suffix (string) – The hook-specific part of the hook name.

Returns:

string