1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link http://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license http://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\models; |
14
|
|
|
|
15
|
|
|
use vistart\Models\queries\BaseEntityQuery; |
16
|
|
|
use vistart\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 2.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
|
60 |
|
public function init() |
36
|
|
|
{ |
37
|
60 |
|
if ($this->skipInit) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
60 |
|
$this->initEntityEvents(); |
41
|
60 |
|
$this->checkAttributes(); |
42
|
60 |
|
parent::init(); |
43
|
60 |
|
} |
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
|
60 |
|
public function checkAttributes() |
53
|
|
|
{ |
54
|
60 |
|
if (YII_ENV !== YII_ENV_PROD || YII_DEBUG) { |
55
|
60 |
|
if (!is_string($this->idAttribute) && empty($this->idAttribute) && |
56
|
60 |
|
!is_string($this->guidAttribute) && empty($this->guidAttribute)) { |
57
|
|
|
$errorInfo = 'ID and GUID attributes are not be disabled simultaneously in relational database.'; |
58
|
|
|
throw new \yii\base\NotSupportedException($errorInfo); |
59
|
|
|
} |
60
|
60 |
|
} |
61
|
60 |
|
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
|
60 |
|
public static function find() |
74
|
|
|
{ |
75
|
60 |
|
$self = static::buildNoInitModel(); |
76
|
60 |
|
if (!is_string($self->queryClass)) { |
77
|
|
|
$self->queryClass = BaseEntityQuery::className(); |
78
|
|
|
} |
79
|
60 |
|
$queryClass = $self->queryClass; |
80
|
60 |
|
return new $queryClass(get_called_class(), ['noInitModel' => $self]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|