ReadRow.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\App\ResourceConnection;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. /**
  10. * Class DeleteRow
  11. */
  12. class ReadRow
  13. {
  14. /**
  15. * @var MetadataPool
  16. */
  17. private $metadataPool;
  18. /**
  19. * @var ResourceConnection
  20. */
  21. private $resourceConnection;
  22. /**
  23. * CreateRow constructor.
  24. *
  25. * @param MetadataPool $metadataPool
  26. * @param ResourceConnection $resourceConnection
  27. */
  28. public function __construct(
  29. MetadataPool $metadataPool,
  30. ResourceConnection $resourceConnection
  31. ) {
  32. $this->metadataPool = $metadataPool;
  33. $this->resourceConnection = $resourceConnection;
  34. }
  35. /**
  36. * @param string $entityType
  37. * @param string $identifier
  38. * @param array $context
  39. * @return array
  40. * @throws \Exception
  41. */
  42. public function execute($entityType, $identifier, $context = [])
  43. {
  44. $metadata = $this->metadataPool->getMetadata($entityType);
  45. $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
  46. $select = $connection->select()
  47. ->from(['t' => $metadata->getEntityTable()])
  48. ->where($metadata->getIdentifierField() . ' = ?', $identifier);
  49. foreach ($context as $field => $value) {
  50. $select->where(
  51. $connection->quoteIdentifier($field) . ' = ?',
  52. $value
  53. );
  54. }
  55. $data = $connection->fetchRow($select);
  56. return $data ?: [];
  57. }
  58. }