-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.php
79 lines (61 loc) · 1.85 KB
/
Entity.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
<?php
/**
* ORM framework
* @author Cristian Lorenzetto <[email protected]>
*/
class Entity implements Serializable
{
private $data;
private $statement;
private $is_new;
public function __construct(Statement $statement,array $c=array(),$new=true){
$this->data=$c;
$this->is_new=$new;
$this->statement=$statement;
}
public function is_new(){
return $this->is_new;
}
public function serialize() {
return $this->data;
}
public function unserialize($data) {
$this->data = $data;
}
public function as_array() {
return $this->data;
}
public function __get($key) {
return $this->data[$key];
}
public function __set($key, $value) {
$exist=isset($this->data[$key]);
if (($exist && $value!=$this->data[$key]) || !$exist) $this->statement->set_dirty_field($id,$key);
$this->data[$key]=$value;
}
public function __isset($key) {
return isset($this->data[$key]);
}
public function id() {
return $this->data[$this->statement->id_column()];
}
public function save(){
if ($this->is_new) {
$id=$this->statement->insert($this->data);
if ($id===false) $this->is_new=false;
else $this->data[$this->statement->id_column()]=$id;
return !$this->is_new;
} else {
$id=$this->statement->id_column();
$dirty_fields=$this->statement->get_dirty_fields($this->data[$id]);
$this->statement->where_id_is($this->data[$this->statement->id_column()])->update($this->data);
}
}
public function delete(){
if ($this->statement->where_id_is($this->data[$this->statement->id_column()])->delete()) $this->is_new=true;
}
public function __toString(){
return json_encode($this->data);
}
}
?>