Skip to content
This repository was archived by the owner on Jan 10, 2022. It is now read-only.

Commit ea2287a

Browse files
committed
fix docs
1 parent b0e4e85 commit ea2287a

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

README.md

+47-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ This extension provides support for ActiveRecord custom records order setup.
1010

1111
For license information check the [LICENSE](LICENSE.md)-file.
1212

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)
1515
[![Build Status](https://travis-ci.org/yii2tech/ar-position.svg?branch=master)](https://travis-ci.org/yii2tech/ar-position)
1616

1717

@@ -40,10 +40,15 @@ Usage
4040

4141
This extension provides support for custom records order setup via column-based position index.
4242

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
4444
support in Yii2. You may attach it to your model class in the following way:
4545

4646
```php
47+
<?php
48+
49+
use yii\db\ActiveRecord;
50+
use yii2tech\ar\position\PositionBehavior;
51+
4752
class Item extends ActiveRecord
4853
{
4954
public function behaviors()
@@ -59,11 +64,13 @@ class Item extends ActiveRecord
5964
```
6065

6166
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`.
6368

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:
6570

6671
```php
72+
<?php
73+
6774
$records = Item::find()->orderBy(['position' => SORT_ASC])->all();
6875
foreach ($records as $record) {
6976
echo $record->position . ', ';
@@ -72,60 +79,71 @@ foreach ($records as $record) {
7279
```
7380

7481

75-
## Position saving <span id="position-saving"></span>
82+
### Position saving <span id="position-saving"></span>
7683

7784
Being attached, behavior automatically fills up `positionAttribute` value for the new record, placing it to the end
7885
of the list:
7986

8087
```php
88+
<?php
89+
8190
echo Item::find()->count(); // outputs: 4
8291

8392
$item = new Item();
8493
$item->save();
8594

86-
echo $item->position // outputs: 5
95+
echo $item->position; // outputs: 5
8796
```
8897

8998
However, you may setup position for the new record explicitly:
9099

91100
```php
101+
<?php
102+
92103
echo Item::find()->count(); // outputs: 4
93104

94105
$item = new Item();
95106
$item->position = 2; // enforce position '2'
96107
$item->save();
97108

98-
echo $item->position // outputs: 2 !!!
109+
echo $item->position; // outputs: 2 !!!
99110
```
100111

101112

102-
## Position switching <span id="position-switching"></span>
113+
### Position switching <span id="position-switching"></span>
103114

104115
Existing record can be moved to another position using following methods:
105116

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.
111122

112123
You may as well change record position through the attribute, provided to `positionAttribute` directly:
113124

114125
```php
126+
<?php
127+
115128
$item = Item::find()->andWhere(['position' => 3])->one();
116129
$item->position = 5; // switch position to '5'
117130
$item->save();
118131
```
119132

120133

121-
## Position in group <span id="position-in-group"></span>
134+
### Position in group <span id="position-in-group"></span>
122135

123136
Sometimes single database entity contains several listings, which require custom ordering, separated logically
124137
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`
126139
can be used:
127140

128141
```php
142+
<?php
143+
144+
use yii\db\ActiveRecord;
145+
use yii2tech\ar\position\PositionBehavior;
146+
129147
class FaqQuestion extends ActiveRecord
130148
{
131149
public function behaviors()
@@ -147,28 +165,32 @@ In this case behavior will use owner values of `groupAttributes` as additional c
147165
calculation and changing:
148166

149167
```php
168+
<?php
169+
150170
echo FaqQuestion::find()->andWhere(['categoryId' => 1])->count(); // outputs: '4'
151171
echo FaqQuestion::find()->andWhere(['categoryId' => 2])->count(); // outputs: '7'
152172

153173
$record = new FaqQuestion();
154174
$record->categoryId = 1;
155175
$record->save();
156-
echo $record->position // outputs: '5'
176+
echo $record->position; // outputs: '5'
157177

158178
$record = new FaqQuestion();
159179
$record->categoryId = 2;
160180
$record->save();
161-
echo $record->position // outputs: '8'
181+
echo $record->position; // outputs: '8'
162182
```
163183

164184

165-
## List navigation <span id="list-navigation"></span>
185+
### List navigation <span id="list-navigation"></span>
166186

167187
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()`
169189
methods to determine if particular record is the first or last one in the list. For example:
170190

171191
```php
192+
<?php
193+
172194
echo Item::find()->count(); // outputs: 10
173195

174196
$firstItem = Item::find()->andWhere(['position' => 1])->one();
@@ -181,10 +203,12 @@ echo $lastItem->getIsLast(); // outputs: true
181203
```
182204

183205
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.
185207
For example:
186208

187209
```php
210+
<?php
211+
188212
$item = Item::find()->andWhere(['position' => 5])->one();
189213

190214
$nextItem = $item->findNext();
@@ -197,6 +221,8 @@ echo $prevItem->position; // outputs: 4
197221
You may as well get the first and the last records in the list. For example:
198222

199223
```php
224+
<?php
225+
200226
echo Item::find()->count(); // outputs: 10
201227
$item = Item::find()->andWhere(['position' => 5])->one();
202228

src/PositionBehavior.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* PositionBehavior allows managing custom order for the records in the database.
1616
* Behavior uses the specific integer field of the database entity to set up position index.
17-
* Due to this the database entity, which the model refers to, must contain field [[positionAttribute]].
17+
* Due to this the database entity, which the model refers to, must contain field {@see positionAttribute}.
1818
*
1919
* ```php
2020
* class Item extends ActiveRecord
@@ -439,7 +439,7 @@ public function beforeUpdate($event)
439439

440440
/**
441441
* This event raises after owner inserted or updated.
442-
* It applies previously set [[positionOnSave]].
442+
* It applies previously set {@see positionOnSave}.
443443
* This event supports other functionality.
444444
* @param ModelEvent $event event instance.
445445
*/

0 commit comments

Comments
 (0)