Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

NextPrevModel   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 22
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getNext() 0 4 1
A getPrev() 0 4 1
1
<?php
2
3
namespace luya\traits;
4
5
use yii\db\ActiveQuery;
6
7
/**
8
 * Trait to us within ActiveRecords to get next and prev (previous) model records of the current
9
 * ActiveRecord object model.
10
 *
11
 * @author Basil Suter <[email protected]>
12
 * @since 1.0.0
13
 */
14
trait NextPrevModel
15
{
16
    /**
17
     * Get the next actrive record of the current id
18
     *
19
     * @return ActiveQuery
20
     */
21
    public function getNext()
22
    {
23
        return self::find()->where(['>', 'id', $this->id])->limit(1)->one();
24
    }
25
    
26
    /**
27
     * Get the previous record of a current id
28
     *
29
     * @return ActiveQuery
30
     */
31
    public function getPrev()
32
    {
33
        return self::find()->where(['<', 'id', $this->id])->orderBy(['id' => SORT_DESC])->limit(1)->one();
34
    }
35
}
36