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
115
116
117
118
119
120
121
<?php
namespace common\models\psources;
use yii\elasticsearch\ActiveRecord;
class LockCreator extends ActiveRecord
{
// 索引名相当于库名
public static function index()
{
return 'suo_cang';
}
// 类别名相当于表名
public static function type()
{
return 'lock_creator';
}
// 属性
public function attributes()
{
$mapConfig = self::mapConfig();
return array_keys($mapConfig['properties']);
}
/**
*[mapConfig mapping配置]
*返回这个模型的映射
*/
public static function mapConfig()
{
return [
'properties' => [
'creator' => ['type' => 'string'],
'create' => [
'type' => 'nested',
'properties' => [
'means' => ['type' => 'long'],
'fix_amount' => [
'type' => 'nested',
'properties' => [
'period' => ['type' => 'long'],
'amount' => ['type' => 'long']
]
],
'total_count' => ['type' => 'long'],
'start_time' => ['type' => 'long'],
'asset_symbol' => ['type' => 'string'],
'asset_exec' => ['type' => 'string']
]
],
'unfreeze_id' => ['type' => 'string'],
'block' => [
'type' => 'nested',
'properties' => [
'hash' => ['type' => 'string'],
'height' => ['type' => 'long'],
'ts' => ['type' => 'long'],
'index' => ['type' => 'long']
]
],
'action_type' => ['type' => 'long'],
'beneficiary' => ['type' => 'string'],
'success' => ['type' => 'string']
]
];
}
public static function mapping()
{
return [
static::type() => self::mapConfig(),
];
}
/**
* 设置(更新)此模型的映射
*/
public static function updateMapping()
{
$db = self::getDb();
$command = $db->createCommand();
if (!$command->indexExists(self::index())) {
$command->createIndex(self::index());
}
$command->setMapping(self::index(), self::type(), self::mapping());
}
//获取此模型的映射
public static function getMapping()
{
$db = self::getDb();
$command = $db->createCommand();
return $command->getMapping();
}
/**
* Delete this model's index
*/
public static function deleteIndex()
{
$db = static::getDb();
$command = $db->createCommand();
$command->deleteIndex(static::index(), static::type());
}
public static function updateRecord($book_id, $columns)
{
try {
$record = self::get($book_id);
foreach ($columns as $key => $value) {
$record->$key = $value;
}
return $record->update();
} catch (\Exception $e) { //handle error here return false; } }
return false;
}
}
}