-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.php
61 lines (54 loc) · 1.53 KB
/
Model.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
<?php
use DB\Cortex;
//use DB\SoftErase;
use DB\SQL\Schema;
abstract class Model extends Cortex
{
//use SoftErase;
public $f3, $db = 'DB';
public $fieldConf = array(
'created_at' => array(
'type' => Schema::DT_TIMESTAMP,
'default' => Schema::DF_CURRENT_TIMESTAMP
),
'updated_at' => array(
'type' => Schema::DT_TIMESTAMP,
'default' => '0-0-0 0:0:0'
)
);
public function __construct()
{
if (property_exists($this, 'fields')) {
$this->fieldConf = array_merge($this->fields, $this->fieldConf);
}
parent::__construct();
$this->f3 = Base::instance();
$onloadHandler = function() {
if(property_exists($this, 'guarded')) {
foreach($this->guarded as $guard) {
unset($guard);
}
}
};
$this->onload($onloadHandler);
}
public function update()
{
$this->touch('updated_at');
return $this;
}
public function save()
{
$app = App::instance();
if(array_key_exists('user_id', $this->fieldConf) && !$this->user_id){
$this->user_id = $app->user()->id?:$app->user()->_id;
}
if(array_key_exists('token', $this->fieldConf)) {
$this->token = generateToken();
}
if(array_key_exists('expires_at', $this->fieldConf)) {
$this->expires_at = generateExpiryDate(24);
}
return parent::save();
}
}