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 
19 {
23  protected $_idFieldName = 'entity_id';
24 
30  protected $_eventPrefix = 'sales_order_collection';
31 
37  protected $_eventObject = 'order_collection';
38 
43 
54  public function __construct(
55  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
56  \Psr\Log\LoggerInterface $logger,
57  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
58  \Magento\Framework\Event\ManagerInterface $eventManager,
59  \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
60  \Magento\Framework\DB\Helper $coreResourceHelper,
61  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
62  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
63  ) {
64  parent::__construct(
65  $entityFactory,
66  $logger,
67  $fetchStrategy,
68  $eventManager,
71  $resource
72  );
73  $this->_coreResourceHelper = $coreResourceHelper;
74  }
75 
81  protected function _construct()
82  {
83  $this->_init(\Magento\Sales\Model\Order::class, \Magento\Sales\Model\ResourceModel\Order::class);
84  $this->addFilterToMap(
85  'entity_id',
86  'main_table.entity_id'
87  )->addFilterToMap(
88  'customer_id',
89  'main_table.customer_id'
90  )->addFilterToMap(
91  'quote_address_id',
92  'main_table.quote_address_id'
93  );
94  }
95 
101  public function addItemCountExpr()
102  {
103  if ($this->_fieldsToSelect === null) {
104  // If we select all fields from table, we need to add column alias
105  $this->getSelect()->columns(['items_count' => 'total_item_count']);
106  } else {
107  $this->addFieldToSelect('total_item_count', 'items_count');
108  }
109  return $this;
110  }
111 
117  public function getSelectCountSql()
118  {
119  /* @var $countSelect \Magento\Framework\DB\Select */
120  $countSelect = parent::getSelectCountSql();
121  $countSelect->resetJoinLeft();
122  return $countSelect;
123  }
124 
132  protected function _getAllIdsSelect($limit = null, $offset = null)
133  {
134  $idsSelect = parent::_getAllIdsSelect($limit, $offset);
135  $idsSelect->resetJoinLeft();
136  return $idsSelect;
137  }
138 
145  protected function _addAddressFields()
146  {
147  $billingAliasName = 'billing_o_a';
148  $shippingAliasName = 'shipping_o_a';
149  $joinTable = $this->getTable('sales_order_address');
150 
151  $this->addFilterToMap(
152  'billing_firstname',
153  $billingAliasName . '.firstname'
154  )->addFilterToMap(
155  'billing_lastname',
156  $billingAliasName . '.lastname'
157  )->addFilterToMap(
158  'billing_telephone',
159  $billingAliasName . '.telephone'
160  )->addFilterToMap(
161  'billing_postcode',
162  $billingAliasName . '.postcode'
163  )->addFilterToMap(
164  'shipping_firstname',
165  $shippingAliasName . '.firstname'
166  )->addFilterToMap(
167  'shipping_lastname',
168  $shippingAliasName . '.lastname'
169  )->addFilterToMap(
170  'shipping_telephone',
171  $shippingAliasName . '.telephone'
172  )->addFilterToMap(
173  'shipping_postcode',
174  $shippingAliasName . '.postcode'
175  );
176 
177  $this->getSelect()->joinLeft(
178  [$billingAliasName => $joinTable],
179  "(main_table.entity_id = {$billingAliasName}.parent_id" .
180  " AND {$billingAliasName}.address_type = 'billing')",
181  [
182  $billingAliasName . '.firstname',
183  $billingAliasName . '.lastname',
184  $billingAliasName . '.telephone',
185  $billingAliasName . '.postcode'
186  ]
187  )->joinLeft(
188  [$shippingAliasName => $joinTable],
189  "(main_table.entity_id = {$shippingAliasName}.parent_id" .
190  " AND {$shippingAliasName}.address_type = 'shipping')",
191  [
192  $shippingAliasName . '.firstname',
193  $shippingAliasName . '.lastname',
194  $shippingAliasName . '.telephone',
195  $shippingAliasName . '.postcode'
196  ]
197  );
198  $this->_coreResourceHelper->prepareColumnsList($this->getSelect());
199  return $this;
200  }
201 
207  public function addAddressFields()
208  {
209  return $this->_addAddressFields();
210  }
211 
221  public function addFieldToSearchFilter($field, $condition = null)
222  {
223  $field = $this->_getMappedField($field);
224  $this->_select->orWhere($this->_getConditionSql($field, $condition));
225  return $this;
226  }
227 
235  public function addAttributeToSearchFilter($attributes, $condition = null)
236  {
237  if (is_array($attributes) && !empty($attributes)) {
238  $this->_addAddressFields();
239 
240  foreach ($attributes as $attribute) {
241  $this->addFieldToSearchFilter($this->_attributeToField($attribute['attribute']), $attribute);
242  }
243  } else {
244  $this->addAttributeToFilter($attributes, $condition);
245  }
246 
247  return $this;
248  }
249 
256  public function addBillingAgreementsFilter($agreements)
257  {
258  $agreements = is_array($agreements) ? $agreements : [$agreements];
259  $this->getSelect()->joinInner(
260  ['sbao' => $this->getTable('sales_billing_agreement_order')],
261  'main_table.entity_id = sbao.order_id',
262  []
263  )->where(
264  'sbao.agreement_id IN(?)',
265  $agreements
266  );
267  return $this;
268  }
269 }
addFilterToMap($filter, $alias, $group='fields')
Definition: AbstractDb.php:798
$resource
Definition: bulk.php:12
$logger
$attributes
Definition: matrix.phtml:13
$connection
Definition: bulk.php:13
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot, \Magento\Framework\DB\Helper $coreResourceHelper, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:54
addAttributeToSearchFilter($attributes, $condition=null)
Definition: Collection.php:235