forked from danbistore/wp-orm
/
wp_orm.php
140 lines (109 loc) · 4.38 KB
/
wp_orm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
class WP_ORM {
public static function insert($entity) {
global $wpdb;
$meta_class = get_class($entity) . '_Meta';
$data = array();
$column_formats = $meta_class::$column_formats;
foreach ( get_object_vars( $entity ) as $key => $value ) {
$data[$key] = is_array($value) ? serialize($value) : $value;
}
$data = array_intersect_key($data, $column_formats);
$data_keys = array_keys($data);
$column_formats = array_merge(array_flip($data_keys), $column_formats);
// $wpdb->insert($meta_class::get_table_name(), $data, $column_formats);
if ($wpdb->insert($meta_class::get_table_name(), $data) == false)
$wpdb->print_error();
return $wpdb->insert_id;
}
public static function get($entity) {
global $wpdb;
$class = get_class($entity);
$meta_class = $class . '_Meta';
$select_sql = 'SELECT * FROM ' . $meta_class::get_table_name();
$where_sql = 'WHERE 1=1';
$data = array();
$column_formats = $meta_class::$column_formats;
foreach ( get_object_vars( $entity ) as $key => $value )
if (isset($column_formats[$key]) && !is_null($value))
$where_sql .= $wpdb->prepare(' AND ' . $key . '=' . $column_formats[$key], $value);
$sql = "$select_sql $where_sql";
$results = $wpdb->get_row($sql, ARRAY_A);
if (!empty($results))
$results = new $class($results);
return $results;
}
public static function select($entity, $orderby = '') {
global $wpdb;
$class = get_class($entity);
$meta_class = $class . '_Meta';
$select_sql = 'SELECT * FROM ' . $meta_class::get_table_name();
$where_sql = 'WHERE 1=1';
$data = array();
$column_formats = $meta_class::$column_formats;
foreach ( get_object_vars( $entity ) as $key => $value )
if (isset($column_formats[$key]) && !is_null($value))
$where_sql .= $wpdb->prepare(' AND ' . $key . '=' . $column_formats[$key], $value);
$order_sql = $orderby == '' ? '' : 'ORDER BY ' . $orderby;
$sql = "$select_sql $where_sql $order_sql";
$results = $wpdb->get_results($sql, ARRAY_A);
if (!empty($results)) {
$list = array();
foreach($results as $row) {
$list[] = new $class($row);
}
$results = $list;
}
return $results;
}
public static function delete($entity) {
global $wpdb;
$meta_class = get_class($entity) . '_Meta';
$column_formats = $meta_class::$column_formats;
$where = array();
$where_format = array();
foreach ( get_object_vars( $entity ) as $key => $value )
if (isset($column_formats[$key]) && !is_null($value)) {
$where[$key] = $value;
$where_format[] = $column_formats[$key];
}
$wpdb->delete($meta_class::get_table_name(), $where, $where_format);
}
public static function update($entity) {
global $wpdb;
$meta_class = get_class($entity) . '_Meta';
$column_formats = $meta_class::$column_formats;
$data = array();
$where = array();
$format = array();
$where_format = array();
foreach ( get_object_vars( $entity ) as $key => $value )
if (isset($column_formats[$key]) && !is_null($value)) {
if ($key == $meta_class::$primary_key) {
$where[$key] = $value;
$where_format[] = $column_formats[$key];
} else {
$data[$key] = is_array($value) ? serialize($value) : $value;
$format[] = $column_formats[$key];
}
}
return $wpdb->update($meta_class::get_table_name(), $data, $where, $format, $where_format);
// return $wpdb->update($meta_class::get_table_name(), $data, $where);
}
}
class WP_Entity {
function __construct($data = array()) {
foreach($data as $key => $value) {
$this->$key = $value;
}
}
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
$this->$property = $value;
return $value;
}
}