AbstractGrid.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel;
  7. use Magento\Framework\DB\Adapter\AdapterInterface;
  8. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  9. /**
  10. * Class AbstractGrid
  11. * @api
  12. * @since 100.0.2
  13. */
  14. abstract class AbstractGrid extends AbstractDb implements GridInterface
  15. {
  16. /**
  17. * @var AdapterInterface
  18. */
  19. protected $connection;
  20. /**
  21. * @var string
  22. */
  23. protected $gridTableName;
  24. /**
  25. * @var string
  26. */
  27. protected $orderTableName = 'sales_order';
  28. /**
  29. * @var string
  30. */
  31. protected $addressTableName = 'sales_order_address';
  32. /**
  33. * Resource initialization
  34. *
  35. * @return void
  36. */
  37. protected function _construct()
  38. {
  39. //
  40. }
  41. /**
  42. * Returns connection
  43. *
  44. * @return AdapterInterface
  45. */
  46. public function getConnection()
  47. {
  48. if (!$this->connection) {
  49. $this->connection = $this->_resources->getConnection('sales');
  50. }
  51. return $this->connection;
  52. }
  53. /**
  54. * Returns grid table name
  55. *
  56. * @return string
  57. */
  58. public function getGridTable()
  59. {
  60. return $this->getTable($this->gridTableName);
  61. }
  62. /**
  63. * Purge grid row
  64. *
  65. * @param int|string $value
  66. * @param null|string $field
  67. * @return int
  68. */
  69. public function purge($value, $field = null)
  70. {
  71. return $this->getConnection()->delete(
  72. $this->getTable($this->gridTableName),
  73. [($field ?: 'entity_id') . ' = ?' => $value]
  74. );
  75. }
  76. /**
  77. * Returns update time of the last row in the grid.
  78. *
  79. * If there are no rows in the grid, default value will be returned.
  80. *
  81. * @param string $default
  82. * @return string
  83. * @deprecated 101.0.0 this method is not used in abstract model but only in single child so
  84. * this deprecation is a part of cleaning abstract classes.
  85. * @see \Magento\Sales\Model\ResourceModel\Provider\UpdatedIdListProvider
  86. */
  87. protected function getLastUpdatedAtValue($default = '0000-00-00 00:00:00')
  88. {
  89. $select = $this->getConnection()->select()
  90. ->from($this->getTable($this->gridTableName), ['updated_at'])
  91. ->order('updated_at DESC')
  92. ->limit(1);
  93. $row = $this->getConnection()->fetchRow($select);
  94. return $row['updated_at'] ?? $default;
  95. }
  96. }