Grid.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer\SaveHandler;
  7. use Magento\Framework\Search\Request\Dimension;
  8. class Grid extends IndexerHandler
  9. {
  10. /**
  11. * @var string[]
  12. */
  13. protected $dataTypes = ['searchable', 'filterable', 'virtual'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function saveIndex($dimensions, \Traversable $documents)
  18. {
  19. foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
  20. $this->insertDocumentsForFilterable($batchDocuments, $dimensions);
  21. }
  22. }
  23. /**
  24. * @param array $documents
  25. * @param Dimension[] $dimensions
  26. * @return void
  27. */
  28. protected function insertDocumentsForFilterable(array $documents, array $dimensions)
  29. {
  30. $onDuplicate = [];
  31. foreach ($this->fields as $field) {
  32. if (in_array($field['type'], $this->dataTypes)) {
  33. $onDuplicate[] = $field['name'];
  34. }
  35. }
  36. $this->connection->insertOnDuplicate(
  37. $this->getTableName($this->dataTypes[1], $dimensions),
  38. $this->prepareFilterableFields($documents),
  39. $onDuplicate
  40. );
  41. }
  42. /**
  43. * @param array $documents
  44. * @return array
  45. */
  46. protected function prepareFilterableFields(array $documents)
  47. {
  48. $insertDocuments = [];
  49. foreach ($documents as $entityId => $document) {
  50. $documentFlat = ['entity_id' => $entityId];
  51. foreach ($this->fields as $field) {
  52. if (in_array($field['type'], $this->dataTypes)) {
  53. $documentFlat[$field['name']] = $document[$field['name']];
  54. }
  55. }
  56. $insertDocuments[] = $documentFlat;
  57. }
  58. return $insertDocuments;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function deleteIndex($dimensions, \Traversable $ids)
  64. {
  65. foreach ($this->batch->getItems($ids, $this->batchSize) as $batchIds) {
  66. $this->connection->delete(
  67. $this->getTableName('filterable', $dimensions),
  68. ['entity_id IN(?)' => $batchIds]
  69. );
  70. }
  71. }
  72. }