Skip to content

Improve QueryInterface::withTypecasting() method #1134

@Tigrov

Description

@Tigrov

Proposed new feature or change

  • to avoid additional database queries to obtain column information for type casting;
  • to be able to specify a custom type for columns for type casting.

A possible solution is to add ability to specify types for casting directly for each column in query results:

$query->withTypecasting([
    '*' => true, // automatically typecast all columns using `PDOStatement::getColumnMeta()`
    'name' => ColumnType::STRING, // abstract type
    'age' => 'int', // database type
    'birthday' => false, // do not typecast
    'is_active' => new BooleanColumn(),
    'created_at' => new DateTimeColumn(),
]);

The types can be presented as

  • instances of ColumnInterface;
  • string names of types (e.g. int, char(20), json, etc);
  • true to automatically detect type using PDOStatement::getColumnMeta();
  • empty value (null, false or '') which means without typecasting.

Also possible to use * as a column name to specify default type for all columns.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions