Grid.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\App\ObjectManager;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\Model\ResourceModel\Db\Context;
  10. use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface;
  11. /**
  12. * Class Grid
  13. */
  14. class Grid extends AbstractGrid
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $gridTableName;
  20. /**
  21. * @var string
  22. */
  23. protected $mainTableName;
  24. /**
  25. * @var string
  26. */
  27. protected $orderIdField;
  28. /**
  29. * @var array
  30. */
  31. protected $joins;
  32. /**
  33. * @var array
  34. */
  35. protected $columns;
  36. /**
  37. * @var NotSyncedDataProviderInterface
  38. */
  39. private $notSyncedDataProvider;
  40. /**
  41. * Order grid rows batch size
  42. */
  43. const BATCH_SIZE = 100;
  44. /**
  45. * @param Context $context
  46. * @param string $mainTableName
  47. * @param string $gridTableName
  48. * @param string $orderIdField
  49. * @param array $joins
  50. * @param array $columns
  51. * @param string $connectionName
  52. * @param NotSyncedDataProviderInterface $notSyncedDataProvider
  53. */
  54. public function __construct(
  55. Context $context,
  56. $mainTableName,
  57. $gridTableName,
  58. $orderIdField,
  59. array $joins = [],
  60. array $columns = [],
  61. $connectionName = null,
  62. NotSyncedDataProviderInterface $notSyncedDataProvider = null
  63. ) {
  64. $this->mainTableName = $mainTableName;
  65. $this->gridTableName = $gridTableName;
  66. $this->orderIdField = $orderIdField;
  67. $this->joins = $joins;
  68. $this->columns = $columns;
  69. $this->notSyncedDataProvider =
  70. $notSyncedDataProvider ?: ObjectManager::getInstance()->get(NotSyncedDataProviderInterface::class);
  71. parent::__construct($context, $connectionName);
  72. }
  73. /**
  74. * Adds new orders to the grid.
  75. *
  76. * Only orders that correspond to $value and $field parameters will be added.
  77. *
  78. * @param int|string $value
  79. * @param null|string $field
  80. * @return \Zend_Db_Statement_Interface
  81. */
  82. public function refresh($value, $field = null)
  83. {
  84. $select = $this->getGridOriginSelect()
  85. ->where(($field ?: $this->mainTableName . '.entity_id') . ' = ?', $value);
  86. $sql = $this->getConnection()
  87. ->insertFromSelect(
  88. $select,
  89. $this->getTable($this->gridTableName),
  90. array_keys($this->columns),
  91. AdapterInterface::INSERT_ON_DUPLICATE
  92. );
  93. $this->addCommitCallback(function () use ($sql) {
  94. $this->getConnection()->query($sql);
  95. });
  96. // need for backward compatibility
  97. return $this->getConnection()->query($sql);
  98. }
  99. /**
  100. * Adds new orders to the grid.
  101. *
  102. * Only orders created/updated since the last method call will be added.
  103. *
  104. * @return void
  105. */
  106. public function refreshBySchedule()
  107. {
  108. $notSyncedIds = $this->notSyncedDataProvider->getIds($this->mainTableName, $this->gridTableName);
  109. foreach (array_chunk($notSyncedIds, self::BATCH_SIZE) as $bunch) {
  110. $select = $this->getGridOriginSelect()->where($this->mainTableName . '.entity_id IN (?)', $bunch);
  111. $fetchResult = $this->getConnection()->fetchAll($select);
  112. $this->getConnection()->insertOnDuplicate(
  113. $this->getTable($this->gridTableName),
  114. $fetchResult,
  115. array_keys($this->columns)
  116. );
  117. }
  118. }
  119. /**
  120. * Get order id field.
  121. *
  122. * @return string
  123. */
  124. public function getOrderIdField()
  125. {
  126. return $this->orderIdField;
  127. }
  128. /**
  129. * Returns select object
  130. *
  131. * @return \Magento\Framework\DB\Select
  132. */
  133. protected function getGridOriginSelect()
  134. {
  135. $select = $this->getConnection()->select()
  136. ->from([$this->mainTableName => $this->getTable($this->mainTableName)], []);
  137. foreach ($this->joins as $joinName => $data) {
  138. $select->joinLeft(
  139. [$joinName => $this->getTable($data['table'])],
  140. sprintf(
  141. '%s.%s = %s.%s',
  142. $this->mainTableName,
  143. $data['origin_column'],
  144. $joinName,
  145. $data['target_column']
  146. ),
  147. []
  148. );
  149. }
  150. $columns = [];
  151. foreach ($this->columns as $key => $value) {
  152. if ($value instanceof \Zend_Db_Expr) {
  153. $columns[$key] = $value;
  154. } else {
  155. $columns[$key] = new \Zend_Db_Expr($value);
  156. }
  157. }
  158. $select->columns($columns);
  159. return $select;
  160. }
  161. }