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.

136 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(false), $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->table() . ' WHERE ' . $this->identify($this->key) . '=' . (int) $id;
  59. return $this->db->query($sql)->fetch();
  60. }
  61. /**
  62. * @param array $data
  63. * @return int Affect row or id of inserted field.
  64. */
  65. public function save($data)
  66. {
  67. $key = isset($data[$this->key]) ? $data[$this->key] : false;
  68. $result = false;
  69. if ($key) {
  70. unset($data[$this->key]);
  71. $result = $this->update($data, $this->identify($this->key) . '=' . (int) $id);
  72. } else {
  73. $result = $this->insert($data);
  74. }
  75. return $result;
  76. }
  77. /**
  78. * @param array $data
  79. * @return int Id of inserted row
  80. */
  81. public function insert($data)
  82. {
  83. if (!$this->db->insert($this->table(false), $data)) {
  84. return false;
  85. }
  86. return $this->getInsertId();
  87. }
  88. /**
  89. * @param array $data
  90. * @param mixed $where
  91. * @return int Number of affected rows
  92. */
  93. public function update($data, $where)
  94. {
  95. return $this->db->update($this->table(false), $data, $where);
  96. }
  97. /**
  98. * @param mixed $where
  99. * @return int Number of affected rows
  100. */
  101. public function delete($where)
  102. {
  103. if (is_int($where)) {
  104. $where = $this->identify($this->key) . '=' . (int) $where;
  105. }
  106. return $this->db->delete($this->table(false), $where);
  107. }
  108. /**
  109. * @param bool $autoescape
  110. * @return string
  111. */
  112. public function table($autoescape = true)
  113. {
  114. if (!$this->table) {
  115. $this->table = substr(strtolower(get_class($this)), 0, -5/*strlen('Model')*/);
  116. }
  117. return $identify ? $this->identify($this->table) : $this->table;
  118. }
  119. }