ReadEntityRow.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\MetadataPool;
  8. /**
  9. * Class ReadEntityRow
  10. */
  11. class ReadEntityRow
  12. {
  13. /**
  14. * @var MetadataPool
  15. */
  16. protected $metadataPool;
  17. /**
  18. * @param MetadataPool $metadataPool
  19. */
  20. public function __construct(
  21. MetadataPool $metadataPool
  22. ) {
  23. $this->metadataPool = $metadataPool;
  24. }
  25. /**
  26. * @param string $entityType
  27. * @param string $identifier
  28. * @param array $context
  29. * @return array
  30. * @throws \Exception
  31. */
  32. public function execute($entityType, $identifier, $context = [])
  33. {
  34. $metadata = $this->metadataPool->getMetadata($entityType);
  35. $select = $metadata->getEntityConnection()->select()
  36. ->from(['t' => $metadata->getEntityTable()])
  37. ->where($metadata->getIdentifierField() . ' = ?', $identifier);
  38. foreach ($context as $field => $value) {
  39. $select->where(
  40. $metadata->getEntityConnection()->quoteIdentifier($field) . ' = ?',
  41. $value
  42. );
  43. }
  44. $data = $metadata->getEntityConnection()->fetchRow($select);
  45. return $data ?: [];
  46. }
  47. }