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 
16 {
20  protected $metadataPool;
21 
31  public function __construct(
32  \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
33  \Psr\Log\LoggerInterface $logger,
34  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
35  \Magento\Framework\Event\ManagerInterface $eventManager,
36  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
37  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
38  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
39  ) {
40  $this->metadataPool = $metadataPool;
41  parent::__construct(
42  $entityFactory,
43  $logger,
44  $fetchStrategy,
45  $eventManager,
47  $resource
48  );
49  }
50 
56  protected function _construct()
57  {
58  $this->_init(\Magento\Downloadable\Model\Link::class, \Magento\Downloadable\Model\ResourceModel\Link::class);
59  }
60 
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 ($product instanceof \Magento\Catalog\Model\Product) {
80  $this->addFieldToFilter('cpe.entity_id', $product->getEntityId());
81  } else {
82  $this->addFieldToFilter('cpe.entity_id', ['in' => $product]);
83  }
84  }
85 
86  return $this;
87  }
88 
95  public function addTitleToResult($storeId = 0)
96  {
97  $ifNullDefaultTitle = $this->getConnection()->getIfNullSql('st.title', 'd.title');
98  $this->getSelect()
99  ->joinLeft(
100  ['d' => $this->getTable('downloadable_link_title')],
101  'd.link_id = main_table.link_id AND d.store_id = 0',
102  ['default_title' => 'title']
103  )->joinLeft(
104  ['st' => $this->getTable('downloadable_link_title')],
105  'st.link_id=main_table.link_id AND st.store_id = ' . (int)$storeId,
106  [
107  'store_title' => 'title',
108  'title' => $ifNullDefaultTitle
109  ]
110  )->order('main_table.sort_order ASC')
111  ->order('title ASC');
112  return $this;
113  }
114 
121  public function addPriceToResult($websiteId)
122  {
123  $ifNullDefaultPrice = $this->getConnection()->getIfNullSql('stp.price', 'dp.price');
124  $this->getSelect()->joinLeft(
125  ['dp' => $this->getTable('downloadable_link_price')],
126  'dp.link_id=main_table.link_id AND dp.website_id = 0',
127  ['default_price' => 'price']
128  )->joinLeft(
129  ['stp' => $this->getTable('downloadable_link_price')],
130  'stp.link_id=main_table.link_id AND stp.website_id = ' . (int)$websiteId,
131  ['website_price' => 'price', 'price' => $ifNullDefaultPrice]
132  );
133 
134  return $this;
135  }
136 }
$resource
Definition: bulk.php:12
$logger
$connection
Definition: bulk.php:13