You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
2.9 KiB

  1. <?php
  2. /**
  3. * Класс модели данных
  4. *
  5. * @copyright NetMonsters <team@netmonsters.ru>
  6. * @link http://netmonsters.ru
  7. * @package Majestic
  8. * @subpackage Model
  9. * @since 2010-02-16
  10. * @version SVN: $Id$
  11. * @filesource $URL$
  12. */
  13. abstract class Model
  14. {
  15. /**
  16. * DbDriver instance
  17. *
  18. * @var DbDriver
  19. */
  20. protected $db;
  21. protected $cache;
  22. protected $table;
  23. protected $connection = 'default';
  24. protected $key = 'id';
  25. public function __construct()
  26. {
  27. $this->db = Db::connect($this->connection);
  28. }
  29. /**
  30. * @return int
  31. */
  32. public function getInsertId()
  33. {
  34. return $this->db->getInsertId($this->table(), $this->key);
  35. }
  36. /**
  37. * @param string $ident
  38. * @return string Quoted identifier.
  39. */
  40. public function identify($ident)
  41. {
  42. return $this->db->quoteIdentifier($ident);
  43. }
  44. /**
  45. * @param mixed $value
  46. * @return string Quoted value.
  47. */
  48. public function quote($value)
  49. {
  50. return $this->db->quote($value);
  51. }
  52. /**
  53. * @param int $id
  54. * @return object
  55. */
  56. public function get($id)
  57. {
  58. $sql = 'SELECT * FROM ' . $this->identify($this->table())
  59. . ' WHERE ' . $this->identify($this->key) . '=' . (int) $id;
  60. return $this->db->query($sql)->fetch();
  61. }
  62. /**
  63. * @param array $data
  64. * @return int Affect row or id of inserted field.
  65. */
  66. public function save($data)
  67. {
  68. $key = isset($data[$this->key]) ? $data[$this->key] : false;
  69. $result = false;
  70. if ($key) {
  71. unset($data[$this->key]);
  72. $result = $this->update($data, $this->identify($this->key) . '=' . (int) $id);
  73. } else {
  74. $result = $this->insert($data);
  75. }
  76. return $result;
  77. }
  78. /**
  79. * @param array $data
  80. * @return int Id of inserted row
  81. */
  82. public function insert($data)
  83. {
  84. if (! $this->db->insert($this->table(), $data)) {
  85. return false;
  86. }
  87. return $this->getInsertId();
  88. }
  89. /**
  90. * @param array $data
  91. * @param mixed $where
  92. * @return int Number of affected rows
  93. */
  94. public function update($data, $where)
  95. {
  96. return $this->db->update($this->table(), $data, $where);
  97. }
  98. /**
  99. * @param mixed $where
  100. * @return int Number of affected rows
  101. */
  102. public function delete($where)
  103. {
  104. if (is_int($where)) {
  105. $where = $this->identify($this->key) . '=' . (int) $where;
  106. }
  107. return $this->db->delete($this->table(), $where);
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function table()
  113. {
  114. if (! $this->table) {
  115. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  116. }
  117. return $this->table;
  118. }
  119. }