Completed
Push — master ( 3c6407...1cd5c1 )
by vistart
13:52
created

BaseEntityModel::compositeGUIDs()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 12
cp 0.75
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 12
nc 6
nop 1
crap 9
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\models;
14
15
use rhosocial\base\models\queries\BaseEntityQuery;
16
use rhosocial\base\models\traits\EntityTrait;
17
use yii\db\ActiveRecord;
18
use yii\base\NotSupportedException;
19
20
/**
21
 * The abstract BaseEntityModel is used for entity model class which associates
22
 * with relational database table.
23
 * Note: the $idAttribute and $guidAttribute are not be assigned to false
24
 * simultaneously, and you should set at least one of them as primary key.
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
abstract class BaseEntityModel extends ActiveRecord
29
{
30
    use EntityTrait;
31
32
    /**
33
     * Initialize new entity.
34
     */
35 342
    public function init()
36
    {
37 342
        if ($this->skipInit) {
38 20
            return;
39
        }
40 342
        $this->initEntityEvents();
41 342
        $this->checkAttributes();
42 342
        parent::init();
43 342
    }
44
45
    /**
46
     * Check whether all properties meet the standards. If you want to disable
47
     * checking, please override this method and return true directly. This
48
     * method runs when environment is not production or disable debug mode.
49
     * @return boolean true if all checks pass.
50
     * @throws NotSupportedException
51
     */
52 342
    public function checkAttributes()
53
    {
54 342
        if (YII_ENV !== YII_ENV_PROD || YII_DEBUG) {
55 342
            if (!is_string($this->idAttribute) && empty($this->idAttribute) &&
56 342
                !is_string($this->guidAttribute) && empty($this->guidAttribute)) {
57 1
                $errorInfo = 'ID and GUID attributes are not be disabled simultaneously in relational database.';
58 1
                throw new \yii\base\NotSupportedException($errorInfo);
59
            }
60
        }
61 342
        return true;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     * ------------
67
     * This static method will take $queryClass property to query class. If it is
68
     * not a string, The [[BaseEntityQuery]] will be taken.
69
     * This static method will build non-init model first, so you wouldn't build it any more.
70
     * @return BaseEntityQuery the newly created [[BaseEntityQuery]]
71
     * or its extended class instance.
72
     */
73 299
    public static function find()
74
    {
75 299
        $self = static::buildNoInitModel();
76 299
        if (!is_string($self->queryClass)) {
77 20
            $self->queryClass = BaseEntityQuery::class;
78
        }
79 299
        $queryClass = $self->queryClass;
80 299
        return new $queryClass(get_called_class(), ['noInitModel' => $self]);
81
    }
82
}
83