Db

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Db
Since
3.0

Class Db

View source

Public Methods

MethodDescription
areColumnTypesCompatible()Returns whether two column type definitions are relatively compatible with each other.
deleteIfExists()Executes a DELETE command, but only if there are any rows to delete.
escapeParam()Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam().
getMaxAllowedValueForNumericColumn()Returns the maximum number allowed for a given column type.
getMinAllowedValueForNumericColumn()Returns the minimum number allowed for a given column type.
getNumericalColumnType()Returns a number column type, taking the min, max, and number of decimal points into account.
getSimplifiedColumnType()Returns a simplified version of a given column type.
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.
isNumericColumnType()Returns whether the given column type is numeric.
isTextualColumnType()Returns whether the given column type is textual.
isTypeSupported()Returns whether a given DB connection’s schema supports a column type.
parseColumnLength()Parses a column type definition and returns just the column length/size.
parseColumnType()Parses a column type definition and returns just the column type, if it can be determined.
parseDateParam()Normalizes date params and then sends them off to parseParam().
parseParam()Parses a query param value and returns a yii\db\QueryInterface::where()-compatible condition.
prepareDateForDb()Prepares a date to be sent to the database.
prepareValueForDb()Prepares a value to be sent to the database.
prepareValuesForDb()Prepares an array or object’s values to be sent to the database.

areColumnTypesCompatible()

Returns whether two column type definitions are relatively compatible with each other.

View source

Arguments

Returns

boolean

Signature

public static boolean areColumnTypesCompatible ( $typeA, $typeB )

deleteIfExists()

Executes a DELETE command, but only if there are any rows to delete.

View source

Arguments

Returns

integer – Number of rows affected by the execution.

Throws

Signature

public static integer deleteIfExists ( \craft\helpers\string $table, $condition = '', array $params = [], craft\db\Connection $db = null )

escapeParam()

Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam().

View source

Arguments

  • $value (string) – The param value.

Returns

string – The escaped param value.

Signature

public static string escapeParam ( \craft\helpers\string $value )

getMaxAllowedValueForNumericColumn()

Returns the maximum number allowed for a given column type.

View source

Arguments

Returns

integer, false – The max allowed number, or false if it can't be determined

Signature

public static integer, false getMaxAllowedValueForNumericColumn ( $columnType )

getMinAllowedValueForNumericColumn()

Returns the minimum number allowed for a given column type.

View source

Arguments

Returns

integer, false – The min allowed number, or false if it can't be determined

Signature

public static integer, false getMinAllowedValueForNumericColumn ( $columnType )

getNumericalColumnType()

Returns a number column type, taking the min, max, and number of decimal points into account.

View source

Arguments

Returns

string

Throws

Signature

public static string getNumericalColumnType ( \craft\helpers\int $min = null, \craft\helpers\int $max = null, \craft\helpers\int $decimals = null )

getSimplifiedColumnType()

Returns a simplified version of a given column type.

View source

Arguments

Returns

string

Signature

public static string getSimplifiedColumnType ( $columnType )

getTextualColumnStorageCapacity()

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

View source

Arguments

Returns

integer, null, false – The storage capacity of the column type in bytes, null if unlimited, or false or it can't be determined.

Signature

public static integer, null, false getTextualColumnStorageCapacity ( \craft\helpers\string $columnType, craft\db\Connection $db = null )

getTextualColumnTypeByContentLength()

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

View source

Arguments

Returns

string

Throws

Signature

public static string getTextualColumnTypeByContentLength ( \craft\helpers\int $contentLength, craft\db\Connection $db = null )

isNumericColumnType()

Returns whether the given column type is numeric.

View source

Arguments

Returns

boolean

Signature

public static boolean isNumericColumnType ( \craft\helpers\string $columnType )

isTextualColumnType()

Returns whether the given column type is textual.

View source

Arguments

Returns

boolean

Signature

public static boolean isTextualColumnType ( \craft\helpers\string $columnType )

isTypeSupported()

Returns whether a given DB connection’s schema supports a column type.

View source

Arguments

Returns

boolean

Signature

public static boolean isTypeSupported ( \craft\helpers\string $type, craft\db\Connection $db = null )

parseColumnLength()

Parses a column type definition and returns just the column length/size.

View source

Arguments

Returns

integer, null

Signature

public static integer, null parseColumnLength ( $columnType )

parseColumnType()

Parses a column type definition and returns just the column type, if it can be determined.

View source

Arguments

Returns

string, null

Signature

public static string, null parseColumnType ( $columnType )

parseDateParam()

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

View source

Arguments

  • $column (string)
  • $value (string, array, DateTime)
  • $defaultOperator (string) – The default operator to apply to the values (can be not, !=, <=, >=, <, >, or =)

Returns

mixed

Signature

public static mixed parseDateParam ( \craft\helpers\string $column, $value, \craft\helpers\string $defaultOperator = '=' )

parseParam()

Parses a query param value and returns a yii\db\QueryInterface::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 craft\helpers\ArrayHelper::toArray()). 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

Arguments

  • $column (string) – The database column that the param is targeting.
  • $value (string, integer, array) – The param value(s).
  • $defaultOperator (string) – The default operator to apply to the values (can be not, !=, <=, >=, <, >, or =)
  • $caseInsensitive (boolean) – Whether the resulting condition should be case-insensitive

Returns

mixed

Signature

public static mixed parseParam ( \craft\helpers\string $column, $value, \craft\helpers\string $defaultOperator = '=', $caseInsensitive = false )

prepareDateForDb()

Prepares a date to be sent to the database.

View source

Arguments

  • $date (mixed) – The date to be prepared

Returns

string, null – The prepped date, or null if it could not be prepared

Signature

public static string, null prepareDateForDb ( $date )

prepareValueForDb()

Prepares a value to be sent to the database.

View source

Arguments

  • $value (mixed) – The value to be prepared

Returns

mixed – The prepped value

Signature

public static mixed prepareValueForDb ( $value )

prepareValuesForDb()

Prepares an array or object’s values to be sent to the database.

View source

Arguments

  • $values (mixed) – The values to be prepared

Returns

array – The prepared values

Signature

public static array prepareValuesForDb ( $values )

Constants

ConstantDescription
SIMPLE_TYPE_NUMERIC
SIMPLE_TYPE_TEXTUAL