yiisoft / yiisoft/db

Improve `QueryInterface::withTypecasting()` method

Open
#1,134 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

severity:BC breaking type:enhancement
Dominant language
PHP
Stars
216
Forks
51
Avg merge
3d 10h
Merged PRs (30d)
2

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating QueryInterface::withTypecasting() and the existing type-casting flow. Review how column metadata and type definitions are currently obtained, then identify tests covering casting behavior. Done should support per-column and wildcard type specifications, automatic metadata detection, and explicitly disabled casting without unnecessary database queries.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.