Решение под Symfony 4 и Doctrine 2
Делаем свой абстрактный слой для базы данных
https://www.doctrine-project.org/projects/doctrine...
Настраиваем конфигурацию для DBAL
# the DBAL wrapperClass option
wrapper_class: App\DBAL\MyConnectionWrapper
https://symfony.com/doc/current/reference/configur...
В кастомном DBAL делаем следущую реализацию, также незабываем перенести все
private
свойства и методы из донора
Doctrine\DBAL\Connection
:
public function update($table, array $data, array $criteria, array $types = [])
{
$columns = $values = $conditions = $set = [];
foreach ($data as $columnName => $value) {
$columns[] = $columnName;
$values[] = $value;
$set[] = $columnName . ' = ?';
}
$this->addCriteriaCondition($criteria, $columns, $values, $conditions);
if (is_string(key($types))) {
$types = $this->extractTypeValues($columns, $types);
}
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $set)
. ' WHERE ' . implode(' AND ', $conditions);
return $this->executeStatement($sql, $values, $types);
}
private function addCriteriaCondition(
array $criteria,
array &$columns,
array &$values,
array &$conditions
): void {
$platform = $this->getDatabasePlatform();
foreach ($criteria as $columnName => $value) {
if ($value === null) {
$conditions[] = $platform->getIsNullExpression($columnName);
continue;
}
if(is_array($value)) {
$this->addInCriteriaCondition($columns, $values, $conditions, $value, $columnName);
continue;
}
$columns[] = $columnName;
$values[] = $value;
$conditions[] = $columnName . ' = ?';
}
}
private function addInCriteriaCondition(
array &$columns,
array &$values,
array &$conditions,
array $valuesArray,
$columnName
) {
$condition = $columnName . ' IN (';
foreach($valuesArray as $index => $value) {
$columns[] = $columnName;
$values[] = $value;
$condition .= '?';
if($index != count($valuesArray) - 1) {
$condition .= ', ';
}
}
$conditions[] = $condition . ')';
}