Craft 3 Class Reference

Class craft\helpers\Db

Inheritance
craft\helpers\Db
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/helpers/Db.php

Class Db

Public Methods
Method Description Defined By
areColumnTypesCompatible() Returns whether two column type definitions are relatively compatible with each other. craft\helpers\Db
escapeParam() Escapes commas and asterisks in a string so they are not treated as special characters in craft\helpers\Db::parseParam(). craft\helpers\Db
getMaxAllowedValueForNumericColumn() Returns the maximum number allowed for a given column type. craft\helpers\Db
getMinAllowedValueForNumericColumn() Returns the minimum number allowed for a given column type. craft\helpers\Db
getNumericalColumnType() Returns a number column type, taking the min, max, and number of decimal points into account. craft\helpers\Db
getSimplifiedColumnType() Returns a simplified version of a given column type. craft\helpers\Db
getTextualColumnStorageCapacity() Returns the maximum number of bytes a given textual column type can hold for a given database. craft\helpers\Db
getTextualColumnTypeByContentLength() Given a length of a piece of content, returns the underlying database column type to use for saving. craft\helpers\Db
isNumericColumnType() Returns whether the given column type is numeric. craft\helpers\Db
isTextualColumnType() Returns whether the given column type is textual. craft\helpers\Db
isTypeSupported() Returns whether a given DB connection’s schema supports a column type. craft\helpers\Db
parseColumnLength() Parses a column type definition and returns just the column length/size. craft\helpers\Db
parseColumnType() Parses a column type definition and returns just the column type, if it can be determined. craft\helpers\Db
parseDateParam() Normalizes date params and then sends them off to parseParam(). craft\helpers\Db
parseParam() Parses a query param value and returns a yii\db\QueryInterface::where()-compatible condition. craft\helpers\Db
prepareDateForDb() Prepares a date to be sent to the database. craft\helpers\Db
prepareValueForDb() Prepares a value to be sent to the database. craft\helpers\Db
prepareValuesForDb() Prepares an array or object’s values to be sent to the database. craft\helpers\Db
Constants
Constant Value Description Defined By
SIMPLE_TYPE_NUMERIC 'numeric' craft\helpers\Db
SIMPLE_TYPE_TEXTUAL 'textual' craft\helpers\Db

Method Details

areColumnTypesCompatible() public static method #

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

public static boolean areColumnTypesCompatible ( $typeA, $typeB )
$typeA string
$typeB string

escapeParam() public static method #

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

public static string escapeParam ( \craft\helpers\string $value )
$value string The param value.
return string The escaped param value.

getMaxAllowedValueForNumericColumn() public static method #

Returns the maximum number allowed for a given column type.

public static integer, false getMaxAllowedValueForNumericColumn ( $columnType )
$columnType string
return integer, false The max allowed number, or false if it can't be determined

getMinAllowedValueForNumericColumn() public static method #

Returns the minimum number allowed for a given column type.

public static integer, false getMinAllowedValueForNumericColumn ( $columnType )
$columnType string
return integer, false The min allowed number, or false if it can't be determined

getNumericalColumnType() public static method #

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

public static string getNumericalColumnType ( \craft\helpers\int $min null, \craft\helpers\int $max null, \craft\helpers\int $decimals null )
$min integer, null
$max integer, null
$decimals integer, null
throws yii\base\Exception if no column types can contain this

getSimplifiedColumnType() public static method #

Returns a simplified version of a given column type.

public static string getSimplifiedColumnType ( $columnType )
$columnType string

getTextualColumnStorageCapacity() public static method #

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

public static integer, null, false getTextualColumnStorageCapacity ( \craft\helpers\string $columnType, craft\db\Connection $db null )
$columnType string The textual column type to check
$db craft\db\Connection, null The database connection
return integer, null, false The storage capacity of the column type in bytes, null if unlimited, or false or it can't be determined.

getTextualColumnTypeByContentLength() public static method #

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

public static string getTextualColumnTypeByContentLength ( \craft\helpers\int $contentLength, craft\db\Connection $db null )
$contentLength integer
$db craft\db\Connection, null The database connection
throws yii\base\Exception if using an unsupported connection type

isNumericColumnType() public static method #

Returns whether the given column type is numeric.

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

isTextualColumnType() public static method #

Returns whether the given column type is textual.

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

isTypeSupported() public static method #

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

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

parseColumnLength() public static method #

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

public static integer, null parseColumnLength ( $columnType )
$columnType string

parseColumnType() public static method #

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

public static string, null parseColumnType ( $columnType )
$columnType string

parseDateParam() public static method #

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

public static mixed parseDateParam ( \craft\helpers\string $column, $value, \craft\helpers\string $defaultOperator '=' )
$column string
$value string, array, DateTime
$defaultOperator string The default operator to apply to the values (can be not, !=, <=, >=, <, >, or =)

parseParam() public static method #

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).

public static mixed parseParam ( \craft\helpers\string $column, $value, \craft\helpers\string $defaultOperator '=' )
$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 =)

prepareDateForDb() public static method #

Prepares a date to be sent to the database.

public static string, null prepareDateForDb ( $date )
$date mixed The date to be prepared
return string, null The prepped date, or null if it could not be prepared

prepareValueForDb() public static method #

Prepares a value to be sent to the database.

public static mixed prepareValueForDb ( $value )
$value mixed The value to be prepared
return mixed The prepped value

prepareValuesForDb() public static method #

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

public static array prepareValuesForDb ( $values )
$values mixed The values to be prepared
return array The prepared values