yiisoft / yiisoft/db

Incorrect typecasting in some cases when select uses equal names

Open
#1,150 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description
public function testWithTypecastingEdgeCase(): void
{
    $this->dropTable('test');

    $db = $this->getSharedConnection();
    $db->createCommand()->createTable('test', [
        'id' => PseudoType::PK,
        'json_col' => ColumnBuilder::json(),
    ])->execute();
    $db->createCommand()->insert('test', ['json_col' => ['a' => 1, 'b' => 2]])->execute();

    $query = $db
        ->select([
            't1.id as json_col',
            't2.json_col',
        ])
        ->from('test t1')
        ->innerJoin('test t2', 't2.id = t1.id');

    $this->assertSame(['json_col' => '{"a":1,"b":2}'], $query->one());

     // Currently, `['json_col' => 0]` is here
    $this->assertSame(['json_col' => ['a' => 1, 'b' => 2]], $query->withTypecasting()->one());
}
Package version

2.0.0

PHP version

any

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

Use testWithTypecastingEdgeCase as the reproduction entry point, then trace withTypecasting() through the query result typecasting path for duplicate selected names. Run the database test suite containing this case and compare the two assertions for the aliased id and JSON column. Done means the untyped result remains the JSON string and the typed result preserves the expected associative array when both selections are named json_col.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.