Skip to content

Commit

Permalink
Fix #19884: Added support Enums in Query Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sk1t0n committed Jul 10, 2023
1 parent d135996 commit 4352b87
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Enh #19841: Allow jQuery 3.7 to be installed (wouter90)
- Enh #19853: Added support for default value for `\yii\helpers\Console::select()` (rhertogh)
- Bug #19868: Added whitespace sanitation for tests, due to updates in ICU 72 (schmunk42)
- Enh #19884: Added support Enums in Query Builder (sk1t0n)


2.0.48.1 May 24, 2023
Expand Down
15 changes: 11 additions & 4 deletions framework/db/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ public function bindValues($values)
$this->pendingParams[$name] = [$value->getValue(), $value->getType()];
$this->params[$name] = $value->getValue();
} else {
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
if ($value instanceof \BackedEnum) {
$value = $value->value;
} elseif ($value instanceof \UnitEnum) {
$value = $value->name;
}
}
$type = $schema->getPdoType($value);
$this->pendingParams[$name] = [$value, $type];
$this->params[$name] = $value;
Expand Down Expand Up @@ -631,23 +638,23 @@ public function delete($table, $condition = '', $params = [])
*
* The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'),
* where name stands for a column name which will be properly quoted by the method, and definition
* stands for the column type which must contain an abstract DB type.
*
* stands for the column type which must contain an abstract DB type.
*
* The method [[QueryBuilder::getColumnType()]] will be called
* to convert the abstract column types to physical ones. For example, `string` will be converted
* as `varchar(255)`, and `string not null` becomes `varchar(255) not null`.
*
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
* inserted into the generated SQL.
*
*
* Example usage:
* ```php
* Yii::$app->db->createCommand()->createTable('post', [
* 'id' => 'pk',
* 'title' => 'string',
* 'text' => 'text',
* 'column_name double precision null default null',
* ]);
* ]);
* ```
*
* @param string $table the name of the table to be created. The name will be properly quoted by the method.
Expand Down
19 changes: 19 additions & 0 deletions tests/framework/db/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1525,4 +1525,23 @@ public function testBindValuesSupportsDeprecatedPDOCastingFormat()
$db->createCommand()->setSql("SELECT :p1")->bindValues([':p1' => [2, \PDO::PARAM_STR]]);
$this->assertTrue(true);
}

public function testBindValuesSupportsEnums()
{
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
$db = $this->getConnection();
$command = $db->createCommand();

$command->setSql('SELECT :p1')->bindValues([':p1' => enums\Status::ACTIVE]);
$this->assertSame('ACTIVE', $command->params[':p1']);

$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeString::ACTIVE]);
$this->assertSame('active', $command->params[':p1']);

$command->setSql('SELECT :p1')->bindValues([':p1' => enums\StatusTypeInt::ACTIVE]);
$this->assertSame(1, $command->params[':p1']);
} else {
$this->markTestSkipped('Enums are not supported in PHP < 8.1');
}
}
}
9 changes: 9 additions & 0 deletions tests/framework/db/enums/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace yiiunit\framework\db\enums;

enum Status
{
case ACTIVE;
case INACTIVE;
}
9 changes: 9 additions & 0 deletions tests/framework/db/enums/StatusTypeInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace yiiunit\framework\db\enums;

enum StatusTypeInt: int
{
case ACTIVE = 1;
case INACTIVE = 0;
}
9 changes: 9 additions & 0 deletions tests/framework/db/enums/StatusTypeString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace yiiunit\framework\db\enums;

enum StatusTypeString: string
{
case ACTIVE = 'active';
case INACTIVE = 'inactive';
}

0 comments on commit 4352b87

Please sign in to comment.