Data.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\ResourceModel\Config;
  7. /**
  8. * Core config data resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Data extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  15. {
  16. /**
  17. * Define main table
  18. *
  19. * @return void
  20. */
  21. protected function _construct()
  22. {
  23. $this->_init('core_config_data', 'config_id');
  24. }
  25. /**
  26. * Convert array to comma separated value
  27. *
  28. * @param \Magento\Framework\Model\AbstractModel $object
  29. * @return $this
  30. */
  31. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  32. {
  33. if (!$object->getId()) {
  34. $this->_checkUnique($object);
  35. }
  36. if (is_array($object->getValue())) {
  37. $object->setValue(join(',', $object->getValue()));
  38. }
  39. return parent::_beforeSave($object);
  40. }
  41. /**
  42. * Validate unique configuration data before save
  43. * Set id to object if exists configuration instead of throw exception
  44. *
  45. * @param \Magento\Framework\Model\AbstractModel $object
  46. * @return $this
  47. */
  48. protected function _checkUnique(\Magento\Framework\Model\AbstractModel $object)
  49. {
  50. $select = $this->getConnection()->select()->from(
  51. $this->getMainTable(),
  52. [$this->getIdFieldName()]
  53. )->where(
  54. 'scope = :scope'
  55. )->where(
  56. 'scope_id = :scope_id'
  57. )->where(
  58. 'path = :path'
  59. );
  60. $bind = [
  61. 'scope' => $object->getScope(),
  62. 'scope_id' => $object->getScopeId(),
  63. 'path' => $object->getPath(),
  64. ];
  65. $configId = $this->getConnection()->fetchOne($select, $bind);
  66. if ($configId) {
  67. $object->setId($configId);
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Clear Scope data
  73. *
  74. * @param string $scopeCode
  75. * @param int|array $scopeIds
  76. * @return void
  77. */
  78. public function clearScopeData($scopeCode, $scopeIds)
  79. {
  80. if (!is_array($scopeIds)) {
  81. $scopeIds = [$scopeIds];
  82. }
  83. $this->getConnection()->delete(
  84. $this->getMainTable(),
  85. ['scope = ?' => $scopeCode, 'scope_id IN (?)' => $scopeIds]
  86. );
  87. }
  88. }