Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for IN (array) condition with update() and delete() #6702

Open
wants to merge 1 commit into
base: 4.3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -370,9 +370,14 @@ private function getCriteriaCondition(array $criteria): array
continue;
}

$columns[] = $columnName;
$values[] = $value;
$conditions[] = $columnName . ' = ?';
if (is_array($value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot guess that an IN query must be used based on the value being an array. This would break types that map to a PHP array like JSON for instance.

$conditions[] = $columnName . ' IN (?)';
} else {
$conditions[] = $columnName . ' = ?';
}

$columns[] = $columnName;
$values[] = $value;
}

return [$columns, $values, $conditions];
@@ -383,8 +388,8 @@ private function getCriteriaCondition(array $criteria): array
*
* Table expression and columns are not escaped and are not safe for user-input.
*
* @param array<string, mixed> $criteria
* @param array<int<0,max>, string|ParameterType|Type>|array<string, string|ParameterType|Type> $types
* @param array<string, mixed> $criteria
* @param array<int<0,max>, WrapperParameterType>|array<string, WrapperParameterType> $types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this is a breaking changes for extending classes, however I think it's not a big deal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be documented though.

*
* @return int|numeric-string The number of affected rows.
*
@@ -447,9 +452,9 @@ public function getTransactionIsolation(): TransactionIsolationLevel
*
* Table expression and columns are not escaped and are not safe for user-input.
*
* @param array<string, mixed> $data
* @param array<string, mixed> $criteria
* @param array<int<0,max>, string|ParameterType|Type>|array<string, string|ParameterType|Type> $types
* @param array<string, mixed> $data
* @param array<string, mixed> $criteria
* @param array<int<0,max>, WrapperParameterType>|array<string, WrapperParameterType> $types
*
* @return int|numeric-string The number of affected rows.
*
@@ -523,10 +528,10 @@ public function insert(string $table, array $data, array $types = []): int|strin
/**
* Extract ordered type list from an ordered column list and type map.
*
* @param array<int, string> $columns
* @param array<int, string|ParameterType|Type>|array<string, string|ParameterType|Type> $types
* @param array<int, string> $columns
* @param array<int, WrapperParameterType>|array<string, WrapperParameterType> $types
*
* @return array<int<0, max>, string|ParameterType|Type>
* @return array<int<0, max>, WrapperParameterType>
*/
private function extractTypeValues(array $columns, array $types): array
{
67 changes: 67 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

namespace Doctrine\DBAL\Tests;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
@@ -431,6 +432,41 @@ public function testUpdateWithIsNull(): void
);
}

public function testUpdateWithInArray(): void
{
$conn = $this->getExecuteStatementMockConnection();

$conn->expects(self::once())
->method('executeStatement')
->with(
'UPDATE TestTable SET text = ? WHERE id IN (?) AND name = ?',
[
'some text',
[1, 2],
'foo',
],
[
'string',
ArrayParameterType::STRING,
'string',
],
);

$conn->update(
'TestTable',
['text' => 'some text'],
[
'id' => [1, 2],
'name' => 'foo',
],
[
'text' => 'string',
'id' => ArrayParameterType::STRING,
'name' => 'string',
],
);
}

public function testDeleteWithIsNull(): void
{
$conn = $this->getExecuteStatementMockConnection();
@@ -456,6 +492,37 @@ public function testDeleteWithIsNull(): void
);
}

public function testDeleteWithInArray(): void
{
$conn = $this->getExecuteStatementMockConnection();

$conn->expects(self::once())
->method('executeStatement')
->with(
'DELETE FROM TestTable WHERE id IN (?) AND name = ?',
[
[1, 2],
'foo',
],
[
ArrayParameterType::STRING,
'string',
],
);

$conn->delete(
'TestTable',
[
'id' => [1, 2],
'name' => 'foo',
],
[
'id' => ArrayParameterType::STRING,
'name' => 'string',
],
);
}

#[DataProvider('fetchModeProvider')]
public function testFetch(string $method, callable $invoke, mixed $expected): void
{