Skip to content

Commit

Permalink
Merge pull request #7823 from spryker/bugfix/te-8457-propel-alpha12-p…
Browse files Browse the repository at this point in the history
…rep-and-release-must-be-an-instance-of-pdostatement-error-on-core-functionality

TE-8457 [Propel Alpha12 prep and release] Must be an instance of PDOStatement error on core functionality
  • Loading branch information
Spryker Release Bot authored Jan 26, 2021
2 parents 8dc10c4 + 0b47ed0 commit ed7d9d2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"spryker/kernel": "^3.48.0",
"spryker/log": "^3.0.0",
"spryker/monolog": "^2.0.0",
"spryker/propel-orm": "^1.9.0",
"spryker/propel-orm": "^1.13.0",
"spryker/symfony": "^3.0.0",
"spryker/transfer": "^3.12.0",
"spryker/util-encoding": "^2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use DateTime;
use Exception;
use PDO;
use PDOStatement;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
use Propel\Runtime\Adapter\AdapterInterface;
use Propel\Runtime\Adapter\Pdo\PgsqlAdapter;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Connection\StatementInterface;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Map\ColumnMap;
use Propel\Runtime\Map\TableMap;
Expand Down Expand Up @@ -96,6 +96,17 @@ public function commit(): bool
return true;
}

/**
* @return bool
*/
public function commitIdentical(): bool
{
$this->insertIdenticalEntities($this->entitiesToInsert);
$this->updateEntities($this->entitiesToUpdate);

return true;
}

/**
* @phpstan-param array<string, array<\Propel\Runtime\ActiveRecord\ActiveRecordInterface>>
*
Expand All @@ -117,6 +128,24 @@ protected function insertEntities(array $entitiesToInsert): void
}
}

/**
* This method will not trigger preSave, preInsert, postInsert and postSave.
* All entities have to be identical in terms of modified columns.
*
* @phpstan-param array<string, array<\Propel\Runtime\ActiveRecord\ActiveRecordInterface>>
*
* @param \Propel\Runtime\ActiveRecord\ActiveRecordInterface[][] $entitiesToInsert
*
* @return void
*/
protected function insertIdenticalEntities(array $entitiesToInsert): void
{
foreach ($entitiesToInsert as $entityClassName => $entities) {
$statement = $this->buildInsertStatementForIdenticalEntities($entityClassName, $entities);
$this->executeStatements([$statement], $entityClassName, 'insert identical');
}
}

/**
* @phpstan-param array<string, array<\Propel\Runtime\ActiveRecord\ActiveRecordInterface>>
*
Expand Down Expand Up @@ -223,7 +252,7 @@ protected function postUpdate(array $entities, ConnectionInterface $connection):
}

/**
* @param \PDOStatement[] $statements
* @param \Propel\Runtime\Connection\StatementInterface[] $statements
* @param string $entityClassName
* @param string $type
*
Expand Down Expand Up @@ -278,7 +307,7 @@ protected function clear(): void
* @param string $entityClassName
* @param array $entities
*
* @return \PDOStatement[]
* @return \Propel\Runtime\Connection\StatementInterface[]
*/
protected function buildInsertStatements(string $entityClassName, array $entities): array
{
Expand Down Expand Up @@ -327,6 +356,59 @@ protected function buildInsertStatements(string $entityClassName, array $entitie
return $statements;
}

/**
* @param string $entityClassName
* @param array $entities
*
* @return \Propel\Runtime\Connection\StatementInterface
*/
protected function buildInsertStatementForIdenticalEntities(string $entityClassName, array $entities): StatementInterface
{
$tableMapClass = $this->getTableMapClass($entityClassName);
$columnMapCollection = $tableMapClass->getColumns();
$adapter = $this->getAdapter();
$requiresPrimaryKeyValue = ($adapter instanceof PgsqlAdapter);

$tableMapClassName = $entityClassName::TABLE_MAP;

$connection = $this->getWriteConnection($entityClassName);
$statements = [];

foreach ($entities as $entity) {
$keyIndex = 0;
$entity = $this->updateDateTimes($entity);
$valuesForInsert = $this->prepareValuesForInsert(
$columnMapCollection,
$tableMapClass,
$tableMapClassName,
$entity,
$requiresPrimaryKeyValue
);

$columnNamesForInsertWithPdoPlaceholder = array_map(function (array $columnDetails) use (&$keyIndex, $tableMapClass) {
if ($columnDetails['columnMap']->isPrimaryKey() && $tableMapClass->getPrimaryKeyMethodInfo() !== null) {
return sprintf('(SELECT nextval(\'%s\'))', $tableMapClass->getPrimaryKeyMethodInfo());
}

return sprintf(':p%d', $keyIndex++);
}, $valuesForInsert);

$sql = sprintf(
'INSERT INTO %s (%s) VALUES (%s);',
$tableMapClass->getName(),
implode(', ', array_keys($columnNamesForInsertWithPdoPlaceholder)),
implode(', ', $columnNamesForInsertWithPdoPlaceholder)
);

$statement = $this->prepareStatement($sql, $connection);
$statement = $this->bindInsertValues($statement, $valuesForInsert);

$statements[] = $statement;
}

return $statements;
}

/**
* @param \Propel\Runtime\Map\ColumnMap[] $columnMapCollection
* @param \Propel\Runtime\Map\TableMap $tableMapClass
Expand Down Expand Up @@ -377,9 +459,9 @@ protected function prepareValuesForInsert(
*
* @throws \Spryker\Zed\Propel\Exception\StatementNotPreparedException
*
* @return \PDOStatement
* @return \Propel\Runtime\Connection\StatementInterface
*/
protected function prepareStatement(string $sql, ConnectionInterface $connection): PDOStatement
protected function prepareStatement(string $sql, ConnectionInterface $connection): StatementInterface
{
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$statement = $connection->prepare($sql);
Expand All @@ -392,12 +474,12 @@ protected function prepareStatement(string $sql, ConnectionInterface $connection
}

/**
* @param \PDOStatement $statement
* @param \Propel\Runtime\Connection\StatementInterface $statement
* @param array $values
*
* @return \PDOStatement
* @return \Propel\Runtime\Connection\StatementInterface
*/
protected function bindInsertValues(PDOStatement $statement, array $values): PDOStatement
protected function bindInsertValues(StatementInterface $statement, array $values): StatementInterface
{
$values = array_filter($values, function (array $columnDetails) {
return !$columnDetails['columnMap']->isPrimaryKey();
Expand Down Expand Up @@ -435,7 +517,7 @@ protected function prepareValuesForSave(ColumnMap $columnMap, array $entityData,
* @param string $entityClassName
* @param array $entities
*
* @return \PDOStatement[]
* @return \Propel\Runtime\Connection\StatementInterface[]
*/
protected function buildUpdateStatements(string $entityClassName, array $entities): array
{
Expand Down Expand Up @@ -521,12 +603,12 @@ protected function prepareValuesForUpdate(
}

/**
* @param \PDOStatement $statement
* @param \Propel\Runtime\Connection\StatementInterface $statement
* @param array $values
*
* @return \PDOStatement
* @return \Propel\Runtime\Connection\StatementInterface
*/
protected function bindUpdateValues(PDOStatement $statement, array $values): PDOStatement
protected function bindUpdateValues(StatementInterface $statement, array $values): StatementInterface
{
foreach (array_values($values) as $index => $value) {
$statement->bindValue(sprintf(':p%d', $index), $value['value'], $value['type']);
Expand Down

0 comments on commit ed7d9d2

Please sign in to comment.