DeleteRow.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\App\ResourceConnection;
  9. /**
  10. * Class DeleteRow
  11. */
  12. class DeleteRow
  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 array $data
  38. * @return int
  39. * @throws \Exception
  40. */
  41. public function execute($entityType, $data)
  42. {
  43. $metadata = $this->metadataPool->getMetadata($entityType);
  44. $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
  45. return $connection->delete(
  46. $metadata->getEntityTable(),
  47. [$metadata->getLinkField() . ' = ?' => $data[$metadata->getLinkField()]]
  48. );
  49. }
  50. }