-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTargetModel.php
114 lines (105 loc) · 3.13 KB
/
TargetModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* @copyright Copyright (c) 2015 Yiister
* @license https://github.com/yiister/yii2-target-model/blob/master/LICENSE
* @link https://github.com/yiister/yii2-target-model
*/
namespace yiister\tm;
use Yii;
use yii\db\ActiveRecord;
/**
* This is the model class for table "{{%yiister_target_model}}".
*
* @property integer $id
* @property string $name
* @property string $class_name
*/
class TargetModel extends \yii\db\ActiveRecord
{
/** @var array of TargetModel attributes */
protected static $identityMap = [];
/** @var array of validator rules */
protected $generatedRules;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%yiister_target_model}}';
}
/**
* @inheritdoc
*/
public function rules()
{
if ($this->generatedRules === null) {
$this->generatedRules = [
[['name', 'class_name'], 'required'],
[['name'], 'string', 'max' => 50],
[['class_name'], 'string', 'max' => 255],
];
$safeAttributes = [];
foreach ($this->getTableSchema()->columns as $column) {
if (in_array($column->name, ['id', 'name', 'class_name']) === false) {
$safeAttributes[] = $column->name;
}
}
if (count($safeAttributes) > 0) {
$this->generatedRules[] = [$safeAttributes, 'safe'];
}
}
return $this->generatedRules;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'class_name' => 'Class name',
];
}
/**
* Create a new TargetModel.
* @param string $className
* @param string $name
* @return static
* @throws \Exception
*/
public static function createModel($className, $name = null)
{
$targetModel = new static;
$targetModel->loadDefaultValues();
$targetModel->class_name = $className;
$targetModel->name = $name !== null ? $name : (new \ReflectionClass($className))->getShortName();
if ($targetModel->save() === false) {
throw new \Exception('Cannot save a new record');
}
return $targetModel;
}
/**
* Get a TargetModel as array by className.
* @param string | ActiveRecord $className
* @return array
* @throws \Exception
*/
public static function getTargetModel($className)
{
if (is_object($className) === true) {
$className = get_class($className);
}
$className = ltrim($className, '\\');
if (isset(static::$identityMap[$className]) === false) {
static::$identityMap[$className] = static::find()
->asArray(true)
->where(['class_name' => $className])
->one();
if (static::$identityMap[$className] === null) {
static::$identityMap[$className] = static::createModel($className)->toArray();
}
}
return static::$identityMap[$className];
}
}