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 extends AbstractGrid
17 {
21  protected $gridTableName;
22 
26  protected $mainTableName;
27 
31  protected $orderIdField;
32 
36  protected $joins;
37 
41  protected $columns;
42 
46  private $notSyncedDataProvider;
47 
51  const BATCH_SIZE = 100;
52 
63  public function __construct(
64  Context $context,
68  array $joins = [],
69  array $columns = [],
70  $connectionName = null,
71  NotSyncedDataProviderInterface $notSyncedDataProvider = null
72  ) {
73  $this->mainTableName = $mainTableName;
74  $this->gridTableName = $gridTableName;
75  $this->orderIdField = $orderIdField;
76  $this->joins = $joins;
77  $this->columns = $columns;
78  $this->notSyncedDataProvider =
79  $notSyncedDataProvider ?: ObjectManager::getInstance()->get(NotSyncedDataProviderInterface::class);
80  parent::__construct($context, $connectionName);
81  }
82 
92  public function refresh($value, $field = null)
93  {
94  $select = $this->getGridOriginSelect()
95  ->where(($field ?: $this->mainTableName . '.entity_id') . ' = ?', $value);
96  $sql = $this->getConnection()
97  ->insertFromSelect(
98  $select,
99  $this->getTable($this->gridTableName),
100  array_keys($this->columns),
102  );
103 
104  $this->addCommitCallback(function () use ($sql) {
105  $this->getConnection()->query($sql);
106  });
107 
108  // need for backward compatibility
109  return $this->getConnection()->query($sql);
110  }
111 
119  public function refreshBySchedule()
120  {
121  $notSyncedIds = $this->notSyncedDataProvider->getIds($this->mainTableName, $this->gridTableName);
122  foreach (array_chunk($notSyncedIds, self::BATCH_SIZE) as $bunch) {
123  $select = $this->getGridOriginSelect()->where($this->mainTableName . '.entity_id IN (?)', $bunch);
124  $fetchResult = $this->getConnection()->fetchAll($select);
125  $this->getConnection()->insertOnDuplicate(
126  $this->getTable($this->gridTableName),
127  $fetchResult,
128  array_keys($this->columns)
129  );
130  }
131  }
132 
138  public function getOrderIdField()
139  {
140  return $this->orderIdField;
141  }
142 
148  protected function getGridOriginSelect()
149  {
150  $select = $this->getConnection()->select()
151  ->from([$this->mainTableName => $this->getTable($this->mainTableName)], []);
152  foreach ($this->joins as $joinName => $data) {
153  $select->joinLeft(
154  [$joinName => $this->getTable($data['table'])],
155  sprintf(
156  '%s.%s = %s.%s',
157  $this->mainTableName,
158  $data['origin_column'],
159  $joinName,
160  $data['target_column']
161  ),
162  []
163  );
164  }
165  $columns = [];
166  foreach ($this->columns as $key => $value) {
167  if ($value instanceof \Zend_Db_Expr) {
168  $columns[$key] = $value;
169  } else {
170  $columns[$key] = new \Zend_Db_Expr($value);
171  }
172  }
173  $select->columns($columns);
174  return $select;
175  }
176 }
__construct(Context $context, $mainTableName, $gridTableName, $orderIdField, array $joins=[], array $columns=[], $connectionName=null, NotSyncedDataProviderInterface $notSyncedDataProvider=null)
Definition: Grid.php:63
refresh($value, $field=null)
Definition: Grid.php:92
$value
Definition: gender.phtml:16