DbHelper

Type
Class
Namespace
Craft
Inherits
Craft\DbHelper
Since
1.0

Class DbHelper

See also http://craftcms.com

View source (opens new window)

# Public Properties

# $columnTypeDefaults

Signature

public static array $columnTypeDefaults = [\Craft\ColumnType::Char => ['maxLength' => 255], \Craft\ColumnType::Varchar => ['maxLength' => 255], \Craft\ColumnType::TinyInt => ['maxLength' => 4], \Craft\ColumnType::SmallInt => ['maxLength' => 6], \Craft\ColumnType::MediumInt => ['maxLength' => 9], \Craft\ColumnType::Int => ['maxLength' => 11], \Craft\ColumnType::BigInt => ['maxLength' => 20], \Craft\ColumnType::Decimal => ['maxLength' => 10, 'decimals' => 2], \Craft\ColumnType::Enum => ['values' => []]]

# Public Methods

Method Description
addTablePrefix() Prepares a table name for Yii to add its table prefix
escapeParam() Escapes commas and asterisks in a string so they are not treated as special characters in {@link DbHelper::parseParam()}.
generateColumnDefinition() Generates the column definition SQL for a column.
getAuditColumnConfig()
getForeignKeyName() Returns a foreign key name based on the table and column names.
getIndexName() Returns an index name based on the table, column names, and whether it should be unique.
getPrimaryKeyName() Returns a primary key name based on the table and column names.
getTextualColumnStorageCapacity() Returns the maximum number of bytes a given textual column type can hold for a given database.
getTextualColumnTypeByContentLength() Given a length of a piece of content, returns the underlying database column type to use for saving.
normalizeAttributeConfig() Normalizes a column's config.
normalizeDbObjectName() Ensures that an object name is within the schema's limit.
parseDateParam() Normalizes date params and then sends them off to parseParam().
parseParam() Parses a query param value and returns a {@link \CDbCommand::where()}-compatible condition.

# addTablePrefix()

DEPRECATED

Deprecated Deprecated in Craft 2.2. Use {@link DbCommand::addTablePrefix() craft()->db->addTablePrefix()} instead.

Prepares a table name for Yii to add its table prefix

View source (opens new window)

Arguments

  • $table (mixed) – The table name or an array of table names

Returns

mixed – The modified table name(s)

Signature

public static mixed addTablePrefix ( $table )

# escapeParam()

Escapes commas and asterisks in a string so they are not treated as special characters in {@link DbHelper::parseParam()}.

View source (opens new window)

Arguments

Returns

string (opens new window) – The escaped param value.

Signature

public static string escapeParam ( $value )

# generateColumnDefinition()

Generates the column definition SQL for a column.

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public static string generateColumnDefinition ( $config )

# getAuditColumnConfig()

Signature

public static array getAuditColumnConfig ( )

# getForeignKeyName()

DEPRECATED

Deprecated Deprecated in Craft 2.2. Use {@link DbCommand::getForeignKeyName() craft()->db->getForeignKeyName()} instead.

Returns a foreign key name based on the table and column names.

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public static string getForeignKeyName ( $table, $columns )

# getIndexName()

DEPRECATED

Deprecated Deprecated in Craft 2.2. Use {@link DbCommand::getIndexName() craft()->db->getIndexName()} instead.

Returns an index name based on the table, column names, and whether it should be unique.

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public static string getIndexName ( $table, $columns, $unique = false )

# getPrimaryKeyName()

DEPRECATED

Deprecated Deprecated in Craft 2.2. Use {@link DbCommand::getPrimaryKeyName() craft()->db->getPrimaryKeyName()} instead.

Returns a primary key name based on the table and column names.

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public static string getPrimaryKeyName ( $table, $columns )

# getTextualColumnStorageCapacity()

Returns the maximum number of bytes a given textual column type can hold for a given database.

View source (opens new window)

Arguments

Returns

integer (opens new window) – The storage capacity of the column type in bytes.

Signature

public static integer getTextualColumnStorageCapacity ( $columnType, $database = 'mysql' )

# getTextualColumnTypeByContentLength()

Given a length of a piece of content, returns the underlying database column type to use for saving.

View source (opens new window)

Arguments

  • $contentLength

Returns

string (opens new window)

Signature

public static string getTextualColumnTypeByContentLength ( $contentLength )

# normalizeAttributeConfig()

Normalizes a column's config. Columns can be defined in 3 ways:

  1. ColumnType::TypeName
  2. array(ColumnType::TypeName [, 'other' => 'settings' ... ] )
  3. array('column' => ColumnType::TypeName [, 'other' => 'settings' ... ] )

This function normalizes on the 3rd, merges in the default config settings for the column type, and renames 'maxLength' to 'length'.

View source (opens new window)

Arguments

Returns

array (opens new window)

Signature

public static array normalizeAttributeConfig ( $config )

# normalizeDbObjectName()

DEPRECATED

Deprecated Deprecated in Craft 2.2. Use {@link DbCommand::trimObjectName() craft()->db->trimObjectName()} instead.

Ensures that an object name is within the schema's limit.

View source (opens new window)

Arguments

Returns

string (opens new window)

Signature

public static string normalizeDbObjectName ( $name )

# parseDateParam()

Normalizes date params and then sends them off to parseParam().

View source (opens new window)

Arguments

Returns

mixed

Signature

public static mixed parseDateParam ( $column, $value, &$params )

# parseParam()

Parses a query param value and returns a {@link \CDbCommand::where()}-compatible condition. If the $value is a string, it will automatically be converted to an array, split on any commas within the string (via {@link ArrayHelper::stringToArray()}). If that is not desired behavior, you can escape the comma with a backslash before it.

The first value can be set to either 'and' or 'or' to define whether all of the values must match, or any. If it’s neither 'and' nor 'or', then 'or' will be assumed.

Values can begin with the operators 'not ', '!=', '<=', '>=', '<', '>', or '='. If they don’t, '=' will be assumed.

Values can also be set to either ':empty:' or ':notempty:' if you want to search for empty or non-empty database values. (An “empty” value is either NULL or an empty string of text).

View source (opens new window)

Arguments

Returns

mixed

Signature

public static mixed parseParam ( $column, $value, &$params )