Config.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\ResourceModel;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. /**
  9. * Core Resource Resource Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Config extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
  16. \Magento\Framework\App\Config\ConfigResource\ConfigInterface
  17. {
  18. /**
  19. * Define main table
  20. *
  21. * @return void
  22. */
  23. protected function _construct()
  24. {
  25. $this->_init('core_config_data', 'config_id');
  26. }
  27. /**
  28. * Save config value
  29. *
  30. * @param string $path
  31. * @param string $value
  32. * @param string $scope
  33. * @param int $scopeId
  34. * @return $this
  35. */
  36. public function saveConfig($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
  37. {
  38. $connection = $this->getConnection();
  39. $select = $connection->select()->from(
  40. $this->getMainTable()
  41. )->where(
  42. 'path = ?',
  43. $path
  44. )->where(
  45. 'scope = ?',
  46. $scope
  47. )->where(
  48. 'scope_id = ?',
  49. $scopeId
  50. );
  51. $row = $connection->fetchRow($select);
  52. $newData = ['scope' => $scope, 'scope_id' => $scopeId, 'path' => $path, 'value' => $value];
  53. if ($row) {
  54. $whereCondition = [$this->getIdFieldName() . '=?' => $row[$this->getIdFieldName()]];
  55. $connection->update($this->getMainTable(), $newData, $whereCondition);
  56. } else {
  57. $connection->insert($this->getMainTable(), $newData);
  58. }
  59. return $this;
  60. }
  61. /**
  62. * Delete config value
  63. *
  64. * @param string $path
  65. * @param string $scope
  66. * @param int $scopeId
  67. * @return $this
  68. */
  69. public function deleteConfig($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
  70. {
  71. $connection = $this->getConnection();
  72. $connection->delete(
  73. $this->getMainTable(),
  74. [
  75. $connection->quoteInto('path = ?', $path),
  76. $connection->quoteInto('scope = ?', $scope),
  77. $connection->quoteInto('scope_id = ?', $scopeId)
  78. ]
  79. );
  80. return $this;
  81. }
  82. }