Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
10 
21 {
27  protected $_subscribersInfoJoinedFlag = false;
28 
34  protected $_problemGrouped = false;
35 
42 
47 
53  protected $_customerView;
54 
60  protected $_loadCustomersDataFlag = false;
61 
72  public function __construct(
73  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
74  \Psr\Log\LoggerInterface $logger,
75  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
76  \Magento\Framework\Event\ManagerInterface $eventManager,
77  CustomerRepository $customerRepository,
78  \Magento\Customer\Helper\View $customerView,
79  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
80  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
81  ) {
82  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
83  $this->customerRepository = $customerRepository;
84  $this->_customerView = $customerView;
85  }
86 
92  protected function _construct()
93  {
94  $this->_init(\Magento\Newsletter\Model\Problem::class, \Magento\Newsletter\Model\ResourceModel\Problem::class);
95  }
96 
103  protected function _setIsLoaded($flag = true)
104  {
105  if (!$flag) {
106  $this->_loadCustomersDataFlag = false;
107  }
108  return parent::_setIsLoaded($flag);
109  }
110 
116  public function addSubscriberInfo()
117  {
118  $this->getSelect()->joinLeft(
119  ['subscriber' => $this->getTable('newsletter_subscriber')],
120  'main_table.subscriber_id = subscriber.subscriber_id',
121  ['subscriber_email', 'customer_id', 'subscriber_status']
122  );
123  $this->addFilterToMap('subscriber_id', 'main_table.subscriber_id');
124  $this->_subscribersInfoJoinedFlag = true;
125 
126  return $this;
127  }
128 
134  public function addQueueInfo()
135  {
136  $this->getSelect()->joinLeft(
137  ['queue' => $this->getTable('newsletter_queue')],
138  'main_table.queue_id = queue.queue_id',
139  ['queue_start_at', 'queue_finish_at']
140  )->joinLeft(
141  ['template' => $this->getTable('newsletter_template')],
142  'queue.template_id = template.template_id',
143  ['template_subject', 'template_code', 'template_sender_name', 'template_sender_email']
144  );
145  return $this;
146  }
147 
153  protected function _addCustomersData()
154  {
155  if ($this->_loadCustomersDataFlag) {
156  return;
157  }
158  $this->_loadCustomersDataFlag = true;
159  foreach ($this->getItems() as $item) {
160  if ($item->getCustomerId()) {
161  $customerId = $item->getCustomerId();
162  try {
163  $customer = $this->customerRepository->getById($customerId);
164  $problems = $this->getItemsByColumnValue('customer_id', $customerId);
165  $customerName = $this->_customerView->getCustomerName($customer);
166  foreach ($problems as $problem) {
167  $problem->setCustomerName($customerName)
168  ->setCustomerFirstName($customer->getFirstname())
169  ->setCustomerLastName($customer->getLastname());
170  }
171  } catch (NoSuchEntityException $e) {
172  // do nothing if customer is not found by id
173  }
174  }
175  }
176  }
177 
185  public function load($printQuery = false, $logQuery = false)
186  {
187  parent::load($printQuery, $logQuery);
188  if ($this->_subscribersInfoJoinedFlag) {
189  $this->_addCustomersData();
190  }
191  return $this;
192  }
193 }
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, CustomerRepository $customerRepository, \Magento\Customer\Helper\View $customerView, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:72
addFilterToMap($filter, $alias, $group='fields')
Definition: AbstractDb.php:798
$customer
Definition: customers.php:11
$resource
Definition: bulk.php:12
$logger
getItemsByColumnValue($column, $value)
Definition: Collection.php:360
$problem
Definition: problems.php:9
$connection
Definition: bulk.php:13