UpdateEntityRow.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Model\ResourceModel\Db;
  7. use Magento\Framework\EntityManager\EntityMetadata;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. /**
  10. * Class ReadEntityRow
  11. */
  12. class UpdateEntityRow
  13. {
  14. /**
  15. * @var MetadataPool
  16. */
  17. protected $metadataPool;
  18. /**
  19. * @param MetadataPool $metadataPool
  20. */
  21. public function __construct(
  22. MetadataPool $metadataPool
  23. ) {
  24. $this->metadataPool = $metadataPool;
  25. }
  26. /**
  27. * @param EntityMetadata $metadata
  28. * @param array $data
  29. * @return array
  30. */
  31. protected function prepareData(EntityMetadata $metadata, $data)
  32. {
  33. $output = [];
  34. foreach ($metadata->getEntityConnection()->describeTable($metadata->getEntityTable()) as $column) {
  35. if ($column['DEFAULT'] == 'CURRENT_TIMESTAMP' || $column['IDENTITY']) {
  36. continue;
  37. }
  38. if (array_key_exists(strtolower($column['COLUMN_NAME']), $data)) {
  39. $output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])];
  40. }
  41. }
  42. return $output;
  43. }
  44. /**
  45. * @param string $entityType
  46. * @param array $data
  47. * @return bool
  48. * @throws \Exception
  49. */
  50. public function execute($entityType, $data)
  51. {
  52. $metadata = $this->metadataPool->getMetadata($entityType);
  53. $connection = $metadata->getEntityConnection();
  54. return $connection->update(
  55. $metadata->getEntityTable(),
  56. $this->prepareData($metadata, $data),
  57. [$metadata->getLinkField() . ' = ?' => $data[$metadata->getLinkField()]]
  58. );
  59. }
  60. }