Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Grid.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Grid
17 {
21  protected $resource;
22 
26  protected $indexerRegistry;
27 
31  protected $flatScopeResolver;
32 
38  public function __construct(
42  ) {
43  $this->resource = $resource;
44  $this->indexerRegistry = $indexerRegistry;
45  $this->flatScopeResolver = $flatScopeResolver;
46  }
47 
55  public function syncCustomerGrid()
56  {
57  $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
58  $customerIds = $this->getCustomerIdsForReindex();
59  if ($customerIds) {
60  $indexer->reindexList($customerIds);
61  }
62  }
63 
71  protected function getCustomerIdsForReindex()
72  {
73  $connection = $this->resource->getConnection();
74  $gridTableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []);
75 
76  $select = $connection->select()
77  ->from($this->resource->getTableName($gridTableName), 'last_visit_at')
78  ->order('last_visit_at DESC')
79  ->limit(1);
80  $lastVisitAt = $connection->query($select)->fetchColumn();
81 
82  $select = $connection->select()
83  ->from($this->resource->getTableName('customer_log'), 'customer_id')
84  ->where('last_login_at > ?', $lastVisitAt);
85 
86  $customerIds = [];
87  foreach ($connection->query($select)->fetchAll() as $row) {
88  $customerIds[] = $row['customer_id'];
89  }
90 
91  return $customerIds;
92  }
93 }
__construct(ResourceConnection $resource, IndexerRegistry $indexerRegistry, FlatScopeResolver $flatScopeResolver)
Definition: Grid.php:38
$connection
Definition: bulk.php:13