Collection.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\ResourceModel\Sample;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. /**
  9. * Downloadable samples resource collection
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  15. {
  16. /**
  17. * @var \Magento\Framework\EntityManager\MetadataPool
  18. * @since 100.1.0
  19. */
  20. protected $metadataPool;
  21. /**
  22. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  23. * @param \Psr\Log\LoggerInterface $logger
  24. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  25. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  26. * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  27. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  28. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
  29. */
  30. public function __construct(
  31. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  32. \Psr\Log\LoggerInterface $logger,
  33. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  34. \Magento\Framework\Event\ManagerInterface $eventManager,
  35. \Magento\Framework\EntityManager\MetadataPool $metadataPool,
  36. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  37. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  38. ) {
  39. $this->metadataPool = $metadataPool;
  40. parent::__construct(
  41. $entityFactory,
  42. $logger,
  43. $fetchStrategy,
  44. $eventManager,
  45. $connection,
  46. $resource
  47. );
  48. }
  49. /**
  50. * Init resource model
  51. *
  52. * @return void
  53. */
  54. protected function _construct()
  55. {
  56. $this->_init(
  57. \Magento\Downloadable\Model\Sample::class,
  58. \Magento\Downloadable\Model\ResourceModel\Sample::class
  59. );
  60. }
  61. /**
  62. * Method for product filter
  63. *
  64. * @param \Magento\Catalog\Model\Product|array|int|null $product
  65. * @return $this
  66. */
  67. public function addProductToFilter($product)
  68. {
  69. if (empty($product)) {
  70. $this->addFieldToFilter('product_id', '');
  71. } else {
  72. $this->join(
  73. ['cpe' => $this->getTable('catalog_product_entity')],
  74. sprintf(
  75. 'cpe.%s = main_table.product_id',
  76. $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
  77. )
  78. );
  79. if (is_array($product)) {
  80. $this->addFieldToFilter('cpe.entity_id', ['in' => $product]);
  81. } else {
  82. $this->addFieldToFilter('cpe.entity_id', $product);
  83. }
  84. }
  85. return $this;
  86. }
  87. /**
  88. * Add title column to select
  89. *
  90. * @param int $storeId
  91. * @return $this
  92. */
  93. public function addTitleToResult($storeId = 0)
  94. {
  95. $ifNullDefaultTitle = $this->getConnection()->getIfNullSql('st.title', 'd.title');
  96. $this->getSelect()->joinLeft(
  97. ['d' => $this->getTable('downloadable_sample_title')],
  98. 'd.sample_id=main_table.sample_id AND d.store_id = 0',
  99. ['default_title' => 'title']
  100. )->joinLeft(
  101. ['st' => $this->getTable('downloadable_sample_title')],
  102. 'st.sample_id=main_table.sample_id AND st.store_id = ' . (int)$storeId,
  103. ['store_title' => 'title', 'title' => $ifNullDefaultTitle]
  104. )->order(
  105. 'main_table.sort_order ASC'
  106. )->order(
  107. 'title ASC'
  108. );
  109. return $this;
  110. }
  111. }