Collection.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\ResourceModel\Order\Item;
  7. use \Magento\Sales\Model\ResourceModel\Order\Collection\AbstractCollection;
  8. /**
  9. * Flat sales order payment collection
  10. *
  11. * @api
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. * @since 100.0.2
  14. */
  15. class Collection extends AbstractCollection implements \Magento\Sales\Api\Data\OrderItemSearchResultInterface
  16. {
  17. /**
  18. * Event prefix
  19. *
  20. * @var string
  21. */
  22. protected $_eventPrefix = 'sales_order_item_collection';
  23. /**
  24. * Event object
  25. *
  26. * @var string
  27. */
  28. protected $_eventObject = 'order_item_collection';
  29. /**
  30. * Order field for setOrderFilter
  31. *
  32. * @var string
  33. */
  34. protected $_orderField = 'order_id';
  35. /**
  36. * Model initialization
  37. *
  38. * @return void
  39. */
  40. protected function _construct()
  41. {
  42. $this->_init(\Magento\Sales\Model\Order\Item::class, \Magento\Sales\Model\ResourceModel\Order\Item::class);
  43. }
  44. /**
  45. * Assign parent items on after collection load
  46. *
  47. * @return $this
  48. */
  49. protected function _afterLoad()
  50. {
  51. parent::_afterLoad();
  52. /**
  53. * Assign parent items
  54. */
  55. foreach ($this as $item) {
  56. $this->_resource->unserializeFields($item);
  57. if ($item->getParentItemId()) {
  58. $item->setParentItem($this->getItemById($item->getParentItemId()));
  59. }
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Set random items order
  65. *
  66. * @return $this
  67. */
  68. public function setRandomOrder()
  69. {
  70. $this->getConnection()->orderRand($this->getSelect());
  71. return $this;
  72. }
  73. /**
  74. * Set filter by item id
  75. *
  76. * @param mixed $item
  77. * @return $this
  78. */
  79. public function addIdFilter($item)
  80. {
  81. if (is_array($item)) {
  82. $this->addFieldToFilter('item_id', ['in' => $item]);
  83. } elseif ($item instanceof \Magento\Sales\Model\Order\Item) {
  84. $this->addFieldToFilter('item_id', $item->getId());
  85. } else {
  86. $this->addFieldToFilter('item_id', $item);
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Filter collection by specified product types
  92. *
  93. * @param array $typeIds
  94. * @return $this
  95. */
  96. public function filterByTypes($typeIds)
  97. {
  98. $this->addFieldToFilter('product_type', ['in' => $typeIds]);
  99. return $this;
  100. }
  101. /**
  102. * Filter collection by parent_item_id
  103. *
  104. * @param int $parentId
  105. * @return $this
  106. */
  107. public function filterByParent($parentId = null)
  108. {
  109. if (empty($parentId)) {
  110. $this->addFieldToFilter('parent_item_id', ['null' => true]);
  111. } else {
  112. $this->addFieldToFilter('parent_item_id', $parentId);
  113. }
  114. return $this;
  115. }
  116. }