UrlRewriteCollection.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * URL rewrite collection
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\UrlRewrite\Model\ResourceModel;
  9. class UrlRewriteCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  10. {
  11. /**
  12. * Store Manager Model
  13. *
  14. * @var \Magento\Store\Model\StoreManagerInterface
  15. */
  16. protected $storeManager;
  17. /**
  18. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  19. * @param \Psr\Log\LoggerInterface $logger
  20. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  21. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  22. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  23. * @param mixed $connection
  24. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  25. */
  26. public function __construct(
  27. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  28. \Psr\Log\LoggerInterface $logger,
  29. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  30. \Magento\Framework\Event\ManagerInterface $eventManager,
  31. \Magento\Store\Model\StoreManagerInterface $storeManager,
  32. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  33. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  34. ) {
  35. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  36. $this->storeManager = $storeManager;
  37. }
  38. /**
  39. * Define resource model
  40. *
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. $this->_init(
  46. \Magento\UrlRewrite\Model\UrlRewrite::class,
  47. \Magento\UrlRewrite\Model\ResourceModel\UrlRewrite::class
  48. );
  49. }
  50. /**
  51. * Filter collections by stores
  52. *
  53. * @param mixed $store
  54. * @param bool $withAdmin
  55. * @return $this
  56. * @api
  57. */
  58. public function addStoreFilter($store, $withAdmin = true)
  59. {
  60. if (!is_array($store)) {
  61. $store = [$this->storeManager->getStore($store)->getId()];
  62. }
  63. if ($withAdmin) {
  64. $store[] = 0;
  65. }
  66. $this->addFieldToFilter('store_id', ['in' => $store]);
  67. return $this;
  68. }
  69. }