DActiveRecord   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 10
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A primaryKeySingle() 0 6 2
A hasOne() 0 6 2
A hasMany() 0 6 2
1
<?php
2
3
namespace dameter\abstracts;
4
5
6
use yii\base\NotSupportedException;
7
use yii\db\ActiveRecord;
8
9
/**
10
 * Class DActiveRecord
11
 * @package dameter\abstracts
12
 * @author Tõnis Ormisson <[email protected]>
13
 */
14
class DActiveRecord extends ActiveRecord
15
{
16
    /** in case we would need to customize stuff */
17
18
    /**
19
     * Get the primary key column as string if the one-column PK
20
     * NB! Always use single column Primary-keys!
21
     * @return string
22
     * @throws NotSupportedException if multi-column PrimaryKey is used
23
     */
24
    public static function primaryKeySingle()
25
    {
26
        if (count(static::primaryKey()) === 1) {
27
            return static::tableName() . "_id";
28
        }
29
        throw new NotSupportedException('Not supported for multi-column primary keys');
30
    }
31
32
33
34
    /** {@inheritdoc} */
35
    public function hasMany($class, $link = null)
36
    {
37
        if (empty($link)) {
38
            $link = [static::primaryKeySingle() => static::primaryKeySingle()];
39
        }
40
        return parent::hasMany($class, $link);
41
    }
42
43
    /** {@inheritdoc} */
44
    public function hasOne($class, $link = null)
45
    {
46
        if (empty($link)) {
47
            $link = [static::primaryKeySingle() => static::primaryKeySingle()];
48
        }
49
        return parent::hasOne($class, $link);
50
    }
51
}