Completed
Push — master ( 69ec8b...199d88 )
by Tõnis
02:10
created

DActiveRecord::primaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace dameter\abstracts;
4
5
6
use yii\base\NotSupportedException;
7
use yii\db\ActiveRecord;
8
9
class DActiveRecord extends ActiveRecord
10
{
11
    /** in case we would need to customize stuff */
12
13
    /**
14
     * Get the primary key column as string if the one-column PK
15
     * NB! Always use single column Primary-keys!
16
     * @return string
17
     * @throws NotSupportedException if multi-column PrimaryKey is used
18
     */
19
    public static function primaryKeySingle()
20
    {
21
        if (count(self::primaryKey()) === 1) {
22
            return static::tableName() . "_id";
23
        }
24
        throw new NotSupportedException('Not supported for multi-column primary keys');
25
    }
26
27
    /**
28
     * @return string[]|void
29
     * @throws NotSupportedException
30
     */
31
    public static function primaryKey()
32
    {
33
        throw new NotSupportedException('use primaryKeySingle() instead');
34
    }
35
36
37
    /** {@inheritdoc} */
38
    public function hasMany($class, $link = null)
39
    {
40
41
        if (empty($link)) {
42
            /** @var DActiveRecord $class */
43
            $link = [$class::primaryKeySingle() => $class::primaryKeySingle()];
44
        }
45
        return parent::hasMany($class, $link);
46
    }
47
48
    /** {@inheritdoc} */
49
    public function hasOne($class, $link = null)
50
    {
51
        if (empty($link)) {
52
            /** @var DActiveRecord $class */
53
            $link = [$class::primaryKeySingle() => $class::primaryKeySingle()];
54
        }
55
        return parent::hasOne($class, $link);
56
    }
57
}