Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Helper.php
Go to the documentation of this file.
1 <?php
13 
15 {
20  public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix = 'reports')
21  {
22  parent::__construct($resource, $modulePrefix);
23  }
24 
34  public function mergeVisitorProductIndex($mainTable, $data, $matchFields)
35  {
36  $result = $this->getConnection()->insertOnDuplicate($mainTable, $data, array_keys($data));
37  return $result;
38  }
39 
43  public function updateReportRatingPos($connection, $type, $column, $mainTable, $aggregationTable)
44  {
45  $periodSubSelect = $connection->select();
46  $ratingSubSelect = $connection->select();
47  $ratingSelect = $connection->select();
48 
49  switch ($type) {
50  case 'year':
51  $periodCol = $connection->getDateFormatSql('t.period', '%Y-01-01');
52  break;
53  case 'month':
54  $periodCol = $connection->getDateFormatSql('t.period', '%Y-%m-01');
55  break;
56  default:
57  $periodCol = 't.period';
58  break;
59  }
60 
61  $columns = [
62  'period' => 't.period',
63  'store_id' => 't.store_id',
64  'product_id' => 't.product_id',
65  'product_name' => 't.product_name',
66  'product_price' => 't.product_price',
67  ];
68 
69  if ($type == 'day') {
70  $columns['id'] = 't.id'; // to speed-up insert on duplicate key update
71  }
72 
73  $cols = array_keys($columns);
74  $cols['total_qty'] = new \Zend_Db_Expr('SUM(t.' . $column . ')');
75  $periodSubSelect->from(
76  ['t' => $mainTable],
77  $cols
78  )->group(
79  ['t.store_id', $periodCol, 't.product_id']
80  )->order(
81  ['t.store_id', $periodCol, 'total_qty DESC']
82  );
83 
84  $cols = $columns;
85  $cols[$column] = 't.total_qty';
86  $cols['rating_pos'] = new \Zend_Db_Expr(
87  "(@pos := IF(t.`store_id` <> @prevStoreId OR {$periodCol} <> @prevPeriod, 1, @pos+1))"
88  );
89  $cols['prevStoreId'] = new \Zend_Db_Expr('(@prevStoreId := t.`store_id`)');
90  $cols['prevPeriod'] = new \Zend_Db_Expr("(@prevPeriod := {$periodCol})");
91  $ratingSubSelect->from($periodSubSelect, $cols);
92 
93  $cols = $columns;
94  $cols['period'] = $periodCol;
95  $cols[$column] = 't.' . $column;
96  $cols['rating_pos'] = 't.rating_pos';
97  $ratingSelect->from($ratingSubSelect, $cols);
98 
99  $sql = $ratingSelect->insertFromSelect($aggregationTable, array_keys($cols));
100  $connection->query("SET @pos = 0, @prevStoreId = -1, @prevPeriod = '0000-00-00'");
101  $connection->query($sql);
102  return $this;
103  }
104 }
__construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix='reports')
Definition: Helper.php:20
$resource
Definition: bulk.php:12
$columns
Definition: default.phtml:15
updateReportRatingPos($connection, $type, $column, $mainTable, $aggregationTable)
Definition: Helper.php:43
$type
Definition: item.phtml:13
mergeVisitorProductIndex($mainTable, $data, $matchFields)
Definition: Helper.php:34
$connection
Definition: bulk.php:13