/models/DataObject/Classificationstore/KeyConfig/Dao.php

https://github.com/pimcore/pimcore · PHP · 143 lines · 81 code · 24 blank · 38 comment · 18 complexity · 443137e350833fcdce89823594d0cb45 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\DataObject\Classificationstore\KeyConfig;
  15. use Pimcore\Model;
  16. /**
  17. * @internal
  18. *
  19. * @property \Pimcore\Model\DataObject\Classificationstore\KeyConfig $model
  20. */
  21. class Dao extends Model\Dao\AbstractDao
  22. {
  23. const TABLE_NAME_KEYS = 'classificationstore_keys';
  24. /**
  25. * Get the data for the object from database for the given id, or from the ID which is set in the object
  26. *
  27. * @param int $id
  28. *
  29. * @throws Model\Exception\NotFoundException
  30. */
  31. public function getById($id = null)
  32. {
  33. if ($id != null) {
  34. $this->model->setId($id);
  35. }
  36. $data = $this->db->fetchAssociative('SELECT * FROM ' . self::TABLE_NAME_KEYS . ' WHERE id = ?', [$this->model->getId()]);
  37. if (!empty($data['id'])) {
  38. $this->assignVariablesToModel($data);
  39. } else {
  40. throw new Model\Exception\NotFoundException('KeyConfig with id: ' . $this->model->getId() . ' does not exist');
  41. }
  42. }
  43. /**
  44. * @param string|null $name
  45. *
  46. * @throws \Exception
  47. */
  48. public function getByName($name = null)
  49. {
  50. if ($name != null) {
  51. $this->model->setName($name);
  52. }
  53. $name = $this->model->getName();
  54. $storeId = $this->model->getStoreId();
  55. $stmt = 'SELECT * FROM ' . self::TABLE_NAME_KEYS . ' WHERE name = ' . $this->db->quote($name) . ' and storeId = ' . $storeId;
  56. $data = $this->db->fetchAssociative($stmt);
  57. if (!empty($data['id'])) {
  58. $this->assignVariablesToModel($data);
  59. } else {
  60. throw new Model\Exception\NotFoundException(sprintf('Classification store key config with name "%s" does not exist.', $name));
  61. }
  62. }
  63. /**
  64. * @throws \Exception
  65. */
  66. public function save()
  67. {
  68. if (!$this->model->getId()) {
  69. $this->create();
  70. }
  71. $this->update();
  72. }
  73. /**
  74. * Deletes object from database
  75. */
  76. public function delete()
  77. {
  78. $this->db->delete(self::TABLE_NAME_KEYS, ['id' => $this->model->getId()]);
  79. }
  80. /**
  81. * @throws \Exception
  82. */
  83. public function update()
  84. {
  85. $ts = time();
  86. $this->model->setModificationDate($ts);
  87. $data = [];
  88. $type = $this->model->getObjectVars();
  89. foreach ($type as $key => $value) {
  90. if (in_array($key, $this->getValidTableColumns(self::TABLE_NAME_KEYS))) {
  91. if (is_bool($value)) {
  92. $value = (int) $value;
  93. if (!$value && $key === 'enabled') {
  94. $this->db->delete(
  95. Model\DataObject\Classificationstore\KeyGroupRelation\Dao::TABLE_NAME_RELATIONS,
  96. ['keyId' => $this->model->getId()]);
  97. }
  98. }
  99. if (is_array($value) || is_object($value)) {
  100. if ($this->model->getType() == 'select') {
  101. $value = json_encode($value);
  102. } else {
  103. $value = \Pimcore\Tool\Serialize::serialize($value);
  104. }
  105. }
  106. $data[$key] = $value;
  107. }
  108. }
  109. $this->db->update(self::TABLE_NAME_KEYS, $data, ['id' => $this->model->getId()]);
  110. }
  111. public function create()
  112. {
  113. $ts = time();
  114. $this->model->setCreationDate($ts);
  115. $this->model->setModificationDate($ts);
  116. $this->db->insert(self::TABLE_NAME_KEYS, []);
  117. $this->model->setId((int) $this->db->lastInsertId());
  118. }
  119. }