Collection.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorization\Model\ResourceModel\Rules;
  7. /**
  8. * Rules collection
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Initialize resource model
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init(
  23. \Magento\Authorization\Model\Rules::class,
  24. \Magento\Authorization\Model\ResourceModel\Rules::class
  25. );
  26. }
  27. /**
  28. * Get rules by role id
  29. *
  30. * @param int $roleId
  31. * @return $this
  32. */
  33. public function getByRoles($roleId)
  34. {
  35. $this->addFieldToFilter('role_id', (int)$roleId);
  36. return $this;
  37. }
  38. /**
  39. * Sort by length
  40. *
  41. * @return $this
  42. */
  43. public function addSortByLength()
  44. {
  45. $length = $this->getConnection()->getLengthSql('{{resource_id}}');
  46. $this->addExpressionFieldToSelect('length', $length, 'resource_id');
  47. $this->getSelect()->order('length ' . \Magento\Framework\DB\Select::SQL_DESC);
  48. return $this;
  49. }
  50. }