-
Notifications
You must be signed in to change notification settings - Fork 16
/
readme_en.txt
312 lines (259 loc) · 7.1 KB
/
readme_en.txt
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
Taggable Behavior
=================
Allows active record model to manage tags.
Installation and configuration
------------------------------
Create a table where you want to store tags and cross-table to store tag-model connections.
You can use sample SQL from `schema.sql` file.
In your ActiveRecord model define `behaviors()` method:
~~~
[php]
function behaviors() {
return array(
'tags' => array(
'class' => 'ext.yiiext.behaviors.model.taggable.ETaggableBehavior',
// Table where tags are stored
'tagTable' => 'Tag',
// Cross-table that stores tag-model connections.
// By default it's your_model_tableTag
'tagBindingTable' => 'PostTag',
// Foreign key field field in cross-table.
// By default it's your_model_tableId
'modelTableFk' => 'post_id',
// tagTableCondition - empty by default. Can be used in cases where e.g. the tag is composed of
// two fields and a custom search expression is needed to find the tag. Example for user table:
// 'tagTableCondition' => new CDbExpression("CONCAT(t.name,' ',t.surname) = :tag "),
// Tag table PK field
'tagTablePk' => 'id',
// Tag name field
'tagTableName' => 'name',
// Tag counter field
// if null (default) does not write tag counts to DB
'tagTableCount' => 'count',
// Tag binding table tag ID
'tagBindingTableTagId' => 'tagId',
// Caching component ID. If false don't use cache.
// Defaults to false.
'cacheID' => 'cache',
// Save nonexisting tags.
// When false, throws exception when saving nonexisting tag.
'createTagsAutomatically' => true,
// Default tag selection criteria
'scope' => array(
'condition' => ' t.user_id = :user_id ',
'params' => array( ':user_id' => Yii::app()->user->id ),
),
// Values to insert to tag table on adding tag
'insertValues' => array(
'user_id' => Yii::app()->user->id,
),
)
);
}
~~~
For using AR model for tags (for example, to bind custom behavior), use EARTaggableBehavior.
To do it add following to your config:
~~~
[php]
return array(
// ...
'import'=>array(
'application.models.*',
'application.components.*',
'ext.yiiext.behaviors.model.taggable.*',
// ...
// other imports
),
// ...
);
~~~
In your AR model implement `behaviors()` method:
~~~
[php]
function behaviors() {
return array(
'tags_with_model' => array(
'class' => 'ext.yiiext.behaviors.model.taggable.EARTaggableBehavior',
// tag table name
'tagTable' => 'Tag',
// tag model class
'tagModel' => 'Tag',
// ...
// other options as shown above
)
);
}
~~~
Methods
-------
### setTags($tags)
Replace model tags with new tags set.
~~~
[php]
$post = new Post();
$post->setTags('tag1, tag2, tag3')->save();
~~~
### addTags($tags) or addTag($tags)
Add one or more tags to existing set.
~~~
[php]
$post->addTags('new1, new2')->save();
~~~
### removeTags($tags) or removeTag($tags)
Remove tags specified (if they do exist).
~~~
[php]
$post->removeTags('new1')->save();
~~~
### removeAllTags()
Remove all tags from the model.
~~~
[php]
$post->removeAllTags()->save();
~~~
### getTags()
Get array of model's tags.
~~~
[php]
$tags = $post->getTags();
foreach($tags as $tag){
echo $tag;
}
~~~
### hasTag($tags) или hasTags($tags)
Returns true if all tags specified are assigned to current model and false otherwise.
~~~
[php]
$post = Post::model()->findByPk(1);
if($post->hasTags("yii, php")){
//…
}
~~~
### getAllTags()
Get all possible tags for this model class.
~~~
[php]
$tags = Post::model()->getAllTags();
foreach($tags as $tag){
echo $tag;
}
~~~
### getAllTagsWithModelsCount()
Get all possible tags with models count for each for this model class.
~~~
[php]
$tags = Post::model()->getAllTagsWithModelsCount();
foreach($tags as $tag){
echo $tag['name']." (".$tag['count'].")";
}
~~~
### taggedWith($tags) или withTags($tags)
Limits AR query to records with all tags specified.
~~~
[php]
$posts = Post::model()->taggedWith('php, yii')->findAll();
$postCount = Post::model()->taggedWith('php, yii')->count();
~~~
### resetAllTagsCache() and resetAllTagsWithModelsCountCache()
could be used to reset getAllTags() or getAllTagsWithModelsCount() cache.
Bonus features
--------------
You can print comma separated tags following way:
~~~
[php]
$post->addTags('new1, new2')->save();
echo $post->tags->toString();
~~~
Using multiple tag groups
-------------------------
You can use multiple tag groups for a single model. For example, we will create
OS and Category tag groups for Software model.
First we need to create DB tables. Two for each group:
~~~
[sql]
/* Tag table */
CREATE TABLE `Os` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `Os_name` (`name`)
);
/* Tag binding table */
CREATE TABLE `PostOs` (
`post_id` INT(10) UNSIGNED NOT NULL,
`osId` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`post_id`,`osId`)
);
/* Tag table */
CREATE TABLE `Category` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `Category_name` (`name`)
);
/* Tag binding table */
CREATE TABLE `PostCategory` (
`post_id` INT(10) UNSIGNED NOT NULL,
`categoryId` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`post_id`,`categoryId`)
);
~~~
Then we are attaching behaviors:
~~~
[php]
return array(
'categories' => array(
'class' => 'ext.yiiext.behaviors.model.taggable.ETaggableBehavior',
'tagTable' => 'Category',
'tagBindingTable' => 'PostCategory',
'tagBindingTableTagId' => 'categoryId',
),
'os' => array(
'class' => 'ext.yiiext.behaviors.model.taggable.ETaggableBehavior',
'tagTable' => 'Os',
'tagBindingTable' => 'PostOs',
'tagBindingTableTagId' => 'osId',
),
);
~~~
That's it. Now we can use it:
~~~
[php]
$soft = Software::model()->findByPk(1);
// fist attached taggable behavior is used by default
// so we can use short syntax instead of $soft->categories->addTag("Antivirus"):
$soft->addTag("Antivirus");
$soft->os->addTag("Windows");
$soft->save();
~~~
Using taggable with CAutoComplete
---------------------------------
~~~
[php]
<?$this->widget('CAutoComplete', array(
'name' => 'tags',
'value' => $model->tags->toString(),
'url'=>'/autocomplete/tags', //path to autocomplete URL
'multiple'=>true,
'mustMatch'=>false,
'matchCase'=>false,
)) ?>
~~~
Saving tags will look like following:
~~~
[php]
function actionUpdate(){
$model = Post::model()->findByPk($_GET['id']);
if(isset($_POST['Post'])){
$model->attributes=$_POST['Post'];
$model->setTags($_POST['tags']);
// if you have multiple tag fields:
// $model->tags1->setTags($_POST['tags1']);
// $model->tags1->setTags($_POST['tags2']);
if($model->save()) $this->redirect(array('index'));
}
$this->render('update',array(
'model'=>$model,
));
}
~~~