UpdateRow.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager\Db;
  7. use Magento\Framework\EntityManager\MetadataPool;
  8. use Magento\Framework\EntityManager\EntityMetadataInterface;
  9. use Magento\Framework\DB\Adapter\AdapterInterface;
  10. use Magento\Framework\App\ResourceConnection;
  11. /**
  12. * Class UpdateRow
  13. */
  14. class UpdateRow
  15. {
  16. /**
  17. * @var MetadataPool
  18. */
  19. private $metadataPool;
  20. /**
  21. * @var ResourceConnection
  22. */
  23. private $resourceConnection;
  24. /**
  25. * CreateRow constructor.
  26. *
  27. * @param MetadataPool $metadataPool
  28. * @param ResourceConnection $resourceConnection
  29. */
  30. public function __construct(
  31. MetadataPool $metadataPool,
  32. ResourceConnection $resourceConnection
  33. ) {
  34. $this->metadataPool = $metadataPool;
  35. $this->resourceConnection = $resourceConnection;
  36. }
  37. /**
  38. * @param EntityMetadataInterface $metadata
  39. * @param AdapterInterface $connection
  40. * @param array $data
  41. * @return array
  42. */
  43. protected function prepareData(EntityMetadataInterface $metadata, AdapterInterface $connection, $data)
  44. {
  45. $output = [];
  46. foreach ($connection->describeTable($metadata->getEntityTable()) as $column) {
  47. $columnName = strtolower($column['COLUMN_NAME']);
  48. if ($this->canNotSetTimeStamp($columnName, $column, $data) || $column['IDENTITY']) {
  49. continue;
  50. }
  51. if (isset($data[$columnName])) {
  52. $output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])];
  53. } elseif (!empty($column['NULLABLE'])) {
  54. $output[strtolower($column['COLUMN_NAME'])] = null;
  55. }
  56. }
  57. if (empty($data[$metadata->getIdentifierField()])) {
  58. $output[$metadata->getIdentifierField()] = $metadata->generateIdentifier();
  59. }
  60. return $output;
  61. }
  62. /**
  63. * Prepares SQL conditions for an update request.
  64. *
  65. * @param EntityMetadataInterface $metadata
  66. * @param AdapterInterface $connection
  67. * @param array $data
  68. *
  69. * @return array
  70. */
  71. private function prepareUpdateConditions(
  72. EntityMetadataInterface $metadata,
  73. AdapterInterface $connection,
  74. $data
  75. ) {
  76. $conditions = [];
  77. $indexList = $connection->getIndexList($metadata->getEntityTable());
  78. $primaryKeyName = $connection->getPrimaryKeyName($metadata->getEntityTable());
  79. foreach ($indexList[$primaryKeyName]['COLUMNS_LIST'] as $linkField) {
  80. if (isset($data[$linkField])) {
  81. $conditions[$linkField . ' = ?'] = $data[$linkField];
  82. }
  83. }
  84. return $conditions;
  85. }
  86. /**
  87. * @param string $columnName
  88. * @param string $column
  89. * @param array $data
  90. * @return bool
  91. */
  92. private function canNotSetTimeStamp($columnName, $column, array $data)
  93. {
  94. return $column['DEFAULT'] == 'CURRENT_TIMESTAMP' && !isset($data[$columnName])
  95. && empty($column['NULLABLE']);
  96. }
  97. /**
  98. * @param string $entityType
  99. * @param array $data
  100. * @return array
  101. */
  102. public function execute($entityType, $data)
  103. {
  104. $metadata = $this->metadataPool->getMetadata($entityType);
  105. $connection = $this->resourceConnection->getConnectionByName(
  106. $metadata->getEntityConnectionName()
  107. );
  108. $connection->update(
  109. $metadata->getEntityTable(),
  110. $this->prepareData($metadata, $connection, $data),
  111. $this->prepareUpdateConditions($metadata, $connection, $data)
  112. );
  113. return $data;
  114. }
  115. }