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 
9 
17 {
22  protected $metadataPool;
23 
33  public function __construct(
34  \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
35  \Psr\Log\LoggerInterface $logger,
36  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
37  \Magento\Framework\Event\ManagerInterface $eventManager,
38  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
39  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
40  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
41  ) {
42  $this->metadataPool = $metadataPool;
43  parent::__construct(
44  $entityFactory,
45  $logger,
46  $fetchStrategy,
47  $eventManager,
49  $resource
50  );
51  }
52 
58  protected function _construct()
59  {
60  $this->_init(
61  \Magento\Downloadable\Model\Sample::class,
62  \Magento\Downloadable\Model\ResourceModel\Sample::class
63  );
64  }
65 
72  public function addProductToFilter($product)
73  {
74  if (empty($product)) {
75  $this->addFieldToFilter('product_id', '');
76  } else {
77  $this->join(
78  ['cpe' => $this->getTable('catalog_product_entity')],
79  sprintf(
80  'cpe.%s = main_table.product_id',
81  $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
82  )
83  );
84  if (is_array($product)) {
85  $this->addFieldToFilter('cpe.entity_id', ['in' => $product]);
86  } else {
87  $this->addFieldToFilter('cpe.entity_id', $product);
88  }
89  }
90 
91  return $this;
92  }
93 
100  public function addTitleToResult($storeId = 0)
101  {
102  $ifNullDefaultTitle = $this->getConnection()->getIfNullSql('st.title', 'd.title');
103  $this->getSelect()->joinLeft(
104  ['d' => $this->getTable('downloadable_sample_title')],
105  'd.sample_id=main_table.sample_id AND d.store_id = 0',
106  ['default_title' => 'title']
107  )->joinLeft(
108  ['st' => $this->getTable('downloadable_sample_title')],
109  'st.sample_id=main_table.sample_id AND st.store_id = ' . (int)$storeId,
110  ['store_title' => 'title', 'title' => $ifNullDefaultTitle]
111  )->order(
112  'main_table.sort_order ASC'
113  )->order(
114  'title ASC'
115  );
116 
117  return $this;
118  }
119 }
__construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\EntityManager\MetadataPool $metadataPool, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:33
$resource
Definition: bulk.php:12
$logger
$connection
Definition: bulk.php:13