@@ -10,8 +10,8 @@ This extension provides support for ActiveRecord custom records order setup.
10
10
11
11
For license information check the [ LICENSE] ( LICENSE.md ) -file.
12
12
13
- [ ![ Latest Stable Version] ( https://poser.pugx.org/ yii2tech/ar-position/v/stable.png )] ( https://packagist.org/packages/yii2tech/ar-position )
14
- [ ![ Total Downloads] ( https://poser.pugx.org/ yii2tech/ar-position/downloads.png )] ( https://packagist.org/packages/yii2tech/ar-position )
13
+ [ ![ Latest Stable Version] ( https://img.shields.io/packagist/v/ yii2tech/ar-position.svg )] ( https://packagist.org/packages/yii2tech/ar-position )
14
+ [ ![ Total Downloads] ( https://img.shields.io/packagist/dt/ yii2tech/ar-position.svg )] ( https://packagist.org/packages/yii2tech/ar-position )
15
15
[ ![ Build Status] ( https://travis-ci.org/yii2tech/ar-position.svg?branch=master )] ( https://travis-ci.org/yii2tech/ar-position )
16
16
17
17
@@ -40,10 +40,15 @@ Usage
40
40
41
41
This extension provides support for custom records order setup via column-based position index.
42
42
43
- This extension provides [[ \yii2tech\ar\position\PositionBehavior]] ActiveRecord behavior for such solution
43
+ This extension provides ` \yii2tech\ar\position\PositionBehavior ` ActiveRecord behavior for such solution
44
44
support in Yii2. You may attach it to your model class in the following way:
45
45
46
46
``` php
47
+ <?php
48
+
49
+ use yii\db\ActiveRecord;
50
+ use yii2tech\ar\position\PositionBehavior;
51
+
47
52
class Item extends ActiveRecord
48
53
{
49
54
public function behaviors()
@@ -59,11 +64,13 @@ class Item extends ActiveRecord
59
64
```
60
65
61
66
Behavior uses the specific integer field of the database entity to set up position index.
62
- Due to this the database entity, which the model refers to, must contain field [[ positionAttribute]] .
67
+ Due to this the database entity, which the model refers to, must contain field ` positionAttribute ` .
63
68
64
- In order to display custom list in correct order you should sort it by [[ positionAttribute]] in ascending mode:
69
+ In order to display custom list in correct order you should sort it by ` positionAttribute ` in ascending mode:
65
70
66
71
``` php
72
+ <?php
73
+
67
74
$records = Item::find()->orderBy(['position' => SORT_ASC])->all();
68
75
foreach ($records as $record) {
69
76
echo $record->position . ', ';
@@ -72,60 +79,71 @@ foreach ($records as $record) {
72
79
```
73
80
74
81
75
- ## Position saving <span id =" position-saving " ></span >
82
+ ### Position saving <span id =" position-saving " ></span >
76
83
77
84
Being attached, behavior automatically fills up ` positionAttribute ` value for the new record, placing it to the end
78
85
of the list:
79
86
80
87
``` php
88
+ <?php
89
+
81
90
echo Item::find()->count(); // outputs: 4
82
91
83
92
$item = new Item();
84
93
$item->save();
85
94
86
- echo $item->position // outputs: 5
95
+ echo $item->position; // outputs: 5
87
96
```
88
97
89
98
However, you may setup position for the new record explicitly:
90
99
91
100
``` php
101
+ <?php
102
+
92
103
echo Item::find()->count(); // outputs: 4
93
104
94
105
$item = new Item();
95
106
$item->position = 2; // enforce position '2'
96
107
$item->save();
97
108
98
- echo $item->position // outputs: 2 !!!
109
+ echo $item->position; // outputs: 2 !!!
99
110
```
100
111
101
112
102
- ## Position switching <span id =" position-switching " ></span >
113
+ ### Position switching <span id =" position-switching " ></span >
103
114
104
115
Existing record can be moved to another position using following methods:
105
116
106
- - [[ movePrev()]] - moves record by one position towards the start of the list.
107
- - [[ moveNext()]] - moves record by one position towards the end of the list.
108
- - [[ moveFirst()]] - moves record to the start of the list.
109
- - [[ moveLast()]] - moves record to the end of the list.
110
- - [[ moveToPosition()]] - moves owner record to the specific position.
117
+ - ` movePrev() ` - moves record by one position towards the start of the list.
118
+ - ` moveNext() ` - moves record by one position towards the end of the list.
119
+ - ` moveFirst() ` - moves record to the start of the list.
120
+ - ` moveLast() ` - moves record to the end of the list.
121
+ - ` moveToPosition() ` - moves owner record to the specific position.
111
122
112
123
You may as well change record position through the attribute, provided to ` positionAttribute ` directly:
113
124
114
125
``` php
126
+ <?php
127
+
115
128
$item = Item::find()->andWhere(['position' => 3])->one();
116
129
$item->position = 5; // switch position to '5'
117
130
$item->save();
118
131
```
119
132
120
133
121
- ## Position in group <span id =" position-in-group " ></span >
134
+ ### Position in group <span id =" position-in-group " ></span >
122
135
123
136
Sometimes single database entity contains several listings, which require custom ordering, separated logically
124
137
by grouping attributes. For example: FAQ questions may be grouped by categories, while inside single category
125
- questions should be ordered manually. For this case [[ \yii2tech\ar\position\PositionBehavior::$groupAttributes]]
138
+ questions should be ordered manually. For this case ` \yii2tech\ar\position\PositionBehavior::$groupAttributes `
126
139
can be used:
127
140
128
141
``` php
142
+ <?php
143
+
144
+ use yii\db\ActiveRecord;
145
+ use yii2tech\ar\position\PositionBehavior;
146
+
129
147
class FaqQuestion extends ActiveRecord
130
148
{
131
149
public function behaviors()
@@ -147,28 +165,32 @@ In this case behavior will use owner values of `groupAttributes` as additional c
147
165
calculation and changing:
148
166
149
167
``` php
168
+ <?php
169
+
150
170
echo FaqQuestion::find()->andWhere(['categoryId' => 1])->count(); // outputs: '4'
151
171
echo FaqQuestion::find()->andWhere(['categoryId' => 2])->count(); // outputs: '7'
152
172
153
173
$record = new FaqQuestion();
154
174
$record->categoryId = 1;
155
175
$record->save();
156
- echo $record->position // outputs: '5'
176
+ echo $record->position; // outputs: '5'
157
177
158
178
$record = new FaqQuestion();
159
179
$record->categoryId = 2;
160
180
$record->save();
161
- echo $record->position // outputs: '8'
181
+ echo $record->position; // outputs: '8'
162
182
```
163
183
164
184
165
- ## List navigation <span id =" list-navigation " ></span >
185
+ ### List navigation <span id =" list-navigation " ></span >
166
186
167
187
Records with custom position order applied make a chained list, which you may navigate if necessary.
168
- You may use [[ \yii2tech\ar\position\PositionBehavior::getIsFirst()]] and [[ \yii2tech\ar\position\PositionBehavior::getIsLast()]]
188
+ You may use ` \yii2tech\ar\position\PositionBehavior::getIsFirst() ` and ` \yii2tech\ar\position\PositionBehavior::getIsLast() `
169
189
methods to determine if particular record is the first or last one in the list. For example:
170
190
171
191
``` php
192
+ <?php
193
+
172
194
echo Item::find()->count(); // outputs: 10
173
195
174
196
$firstItem = Item::find()->andWhere(['position' => 1])->one();
@@ -181,10 +203,12 @@ echo $lastItem->getIsLast(); // outputs: true
181
203
```
182
204
183
205
Having a particular record instance, you can always find record, which is located at next or previous position to it,
184
- using [[ \yii2tech\ar\position\PositionBehavior::getNext()]] or [[ \yii2tech\ar\position\PositionBehavior::getPrev()]] method.
206
+ using ` \yii2tech\ar\position\PositionBehavior::getNext() ` or ` \yii2tech\ar\position\PositionBehavior::getPrev() ` method.
185
207
For example:
186
208
187
209
``` php
210
+ <?php
211
+
188
212
$item = Item::find()->andWhere(['position' => 5])->one();
189
213
190
214
$nextItem = $item->findNext();
@@ -197,6 +221,8 @@ echo $prevItem->position; // outputs: 4
197
221
You may as well get the first and the last records in the list. For example:
198
222
199
223
``` php
224
+ <?php
225
+
200
226
echo Item::find()->count(); // outputs: 10
201
227
$item = Item::find()->andWhere(['position' => 5])->one();
202
228
0 commit comments