ActiveRecord   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 12
c 2
b 0
f 1
dl 0
loc 38
rs 10
wmc 6

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 andmemasin\myabstract;
4
5
use andmemasin\myabstract\traits\ConsoleAwareTrait;
6
use yii\db\ActiveRecord as BaseActiveRecord;
7
use yii\base\NotSupportedException;
8
9
/**
10
 * Class ActiveRecord
11
 * @package andmemasin\myabstract
12
 * @author Tõnis Ormisson <[email protected]>
13
 */
14
class ActiveRecord extends BaseActiveRecord
15
{
16
    use ConsoleAwareTrait;
17
18
    /** @var bool  */
19
    public $isSearchModel = false;
20
21
    /**
22
     * Get the primary key column as string if the one-column PK
23
     * NB! Always use single column Primary-keys!
24
     * NB! this assumes that primary key always has the table_name_id format
25
     * @return string
26
     * @throws NotSupportedException if multi-column PrimaryKey is used
27
     */
28
    public function primaryKeySingle()
29
    {
30
        if (count(static::primaryKey()) === 1) {
31
            return static::primaryKey()[0];
32
        }
33
        throw new NotSupportedException('Not supported for multi-column primary keys');
34
    }
35
36
    /** {@inheritdoc} */
37
    public function hasMany($class, $link = null)
38
    {
39
        if (empty($link)) {
40
            $link = [$this->primaryKeySingle() => $this->primaryKeySingle()];
41
        }
42
        return parent::hasMany($class, $link);
43
    }
44
45
    /** {@inheritdoc} */
46
    public function hasOne($class, $link = null)
47
    {
48
        if (empty($link)) {
49
            $link = [$this->primaryKeySingle() => $this->primaryKeySingle()];
50
        }
51
        return parent::hasOne($class, $link);
52
    }
53
54
}