-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTagModel.php
More file actions
147 lines (113 loc) · 3.36 KB
/
TagModel.php
File metadata and controls
147 lines (113 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// CREATE TABLE `tag` (
// `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
// `name` char(32) NOT NULL DEFAULT '',
// `slug` char(128) NOT NULL DEFAULT '',
// `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
// PRIMARY KEY (`id`),
// UNIQUE KEY `name` (`name`),
// UNIQUE KEY `slug` (`slug`)
// ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
// CREATE TABLE `relationships` (
// `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
// `tagid` smallint(6) NOT NULL DEFAULT '0',
// `objectid` int(10) NOT NULL DEFAULT '0',
// PRIMARY KEY (`id`),
// KEY `tagid` (`tagid`),
// KEY `objectid` (`objectid`)
// ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
class TagModel extends MY_Model {
/**
* 表名
*/
protected $table = 'tag';
/**
* 构造方法
*/
public function __construct() {
parent::__construct();
// required mb_substr function
if( ! function_exists('mb_substr') ) {
throw new Exception('Function mb_substr Not Loaded');
}
}
/**
* 添加名称
*/
public function addName($name, $slug = NULL) {
$from = is_null($slug) ? $name : $slug;
$this->db->insert($this->table, array(
'name' => $this->getName($name),
'slug' => $this->getSlug($from)
));
return $this->db->insert_id();
}
/**
* 根据ID修改名称
*/
public function updateNameById($id, $name) {
$id = intval($id);
if( $this->hasName($name, $id) ) {
return FALSE;
}
$data = array('name'=>$this->getName($name));
$this->db->update($this->table, $data, array('id'=>$id));
return (bool) $this->db->affected_rows();
}
/**
* 根据ID修改别名
*/
public function updateSlugById($id, $slug) {
$id = intval($id);
if( $this->hasSlug($slug, $id) ) {
return FALSE;
}
$data = array('slug'=>$this->getSlug($slug));
$this->db->update($this->table, $data, array('id'=>$id));
return (bool) $this->db->affected_rows();
}
/**
* 删除
*/
public function deleteById($id) {
$this->db->delete($this->table, array('id'=>intval($id)));
if( ! $this->db->affected_rows() ) {
return FALSE;
}
return TRUE;
}
/**
* 检测名称是否存在
*/
public function hasName($name, $ignoreId = NULL) {
if( ! is_null($ignoreId) ) {
$this->db->where('id !=', intval($ignoreId));
}
$this->db->where('name', $this->getName($name));
return (bool) $this->db->get($this->table)->num_rows();
}
/**
* 检测别名是否存在
*/
public function hasSlug($slug, $ignoreId = NULL) {
if( ! is_null($ignoreId) ) {
$this->db->where('id !=', intval($ignoreId));
}
$this->db->where('slug', $this->getSlug($slug));
return (bool) $this->db->get($this->table)->num_rows();
}
/**
* 获取名称
*/
protected function getName($name) {
return mb_substr(trim($name), 0, 32);
}
/**
* 获取别名
*/
protected function getSlug($name) {
$slug = str_replace(' ', '-', trim($name));
$slug = rawurlencode(strtolower($slug));
return mb_substr($slug, 0, 128);
}
}