forked from vishnugopal/active-record-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.php
191 lines (157 loc) · 6.09 KB
/
base.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace ActiveRecord;
require 'active_support/inflector.php';
class Base {
protected static $database;
protected static $table;
protected static $primary_key_field = 'id';
protected static $associations = array();
protected static $association_types = array(
'has_many',
'belongs_to'
);
protected $association_key;
public static function establish_connection($db_settings_name) {
$obj_db = new \PDO(
'mysql:host=' . $db_settings_name['host'] .
';dbname=' . $db_settings_name['database'],
$db_settings_name['username'],
$db_settings_name['password']
);
$obj_db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
static::$database = $obj_db;
}
protected static function table_name() {
return isset(static::$table_name) ?: \ActiveSupport\Inflector::pluralize(static::$class);
}
protected static function primary_key_field() {
return static::$primary_key_field;
}
protected static function associations() {
return static::$associations;
}
protected static function associationsType() {
return static::$association_types;
}
protected static function database() {
return static::$database;
}
public static function find_all($options = array()) {
$sql = 'SELECT * from ' . self::table_name() .
(isset($options['conditions']) ? ' WHERE ' . $options['conditions'] : '') .
(isset($options['limit']) ? ' LIMIT ' . $options['limit'] : '') . ';';
$statement = self::database()->prepare($sql);
$statement->execute();
return self::to_objects($statement->fetchAll(\PDO::FETCH_ASSOC));
}
private static function to_objects($result_set_array) {
$object_list = array();
foreach($result_set_array as $result_set) {
$object_list[] = self::to_object($result_set);
}
return $object_list;
}
private static function to_object($result_set) {
$object = new static::$class;
$object->from_array($result_set);
return $object;
}
/* Instance Methods */
protected $row;
public function __construct() {
}
public function from_array($result_set) {
$this->row = $result_set;
}
public function save() {
if(!isset($this->row) || 0 == count($this->row)) {
throw new \Exception("Can't save empty record.");
}
$sql_fields = '';
foreach($this->row as $key => $value) {
$value = self::database()->quote($value);
$sql_fields .= $key . ' = ' . $value . ', ';
}
$sql_fields = substr($sql_fields, 0, strlen($sql_fields) - 2);
if(!isset($this->row[static::primary_key_field()])) {
throw new \Exception("Primary key not set for row, cannot save.");
}
$primary_key_value = $this->row[static::primary_key_field()];
$sql = 'UPDATE ' . static::table_name() . ' SET ' . $sql_fields .
' WHERE ' . static::primary_key_field() . ' = ' . $primary_key_value;
return $this->database()->exec($sql);
}
public function __call($method, $arguments) {
if(isset($this->row[$method])) {
return $this->row[$method];
} elseif("_set" == substr($method, -4)) {
if((1 != count($arguments)) || !is_scalar($arguments[0])) {
throw new \Exception("Must have one (and just one) scalar value to set.");
}
$property = substr($method, 0, strlen($method) - 4);
if($this->columnExist($property)){
$this->row[$property] = $arguments[0];
} else {
throw new \Exception("The method is undefined");
}
} elseif("_get" == substr($method, -4)) {
if(0 != count($arguments) ) {
throw new \Exception("This method dosn't take arguments");
}
$property = substr($method, 0, strlen($method) - 4);
if($this->columnExist($property)){
return $this->row[$property];
} else {
throw new \Exception("The method is undefined");
}
} elseif($this->association_exists($method)) {
return $this->association_find($method);
} else {
throw new \Exception("Property not found in record.");
}
}
protected function association_exists($association_name) {
$associations = $this->associations();
foreach ($this->associationsType() as $association) {
if(isset($associations[$association])){
if(isset($associations[$association][$association_name]) ||
in_array($association_name, $associations[$association])){
$this->association_key = $association;
return TRUE;
}
}
}
return FALSE;
}
protected function association_foreign_key($association_name) {
$associations = $this->associations();
return isset($associations[$this->association_key][$association_name]['foreign_key_field'])
? $associations[$this->association_key][$association_name]['foreign_key_field']
: strtolower(static::$class) . '_id';
}
protected function association_table_name($association_name) {
$associations = $this->associations();
return isset($associations[$this->association_key][$association_name]['table_name'])
? $associations[$this->association_key][$association_name]['table_name']
: $association_name;
}
protected function association_model($association_name) {
$associations = $this->associations();
return isset($associations[$this->association_key][$association_name]['model'])
? $associations[$this->association_key][$association_name]['model']
: ucwords(\ActiveSupport\Inflector::singularize($association_name));
}
protected function association_find($association_name) {
$primary_key_field = static::primary_key_field();
$primary_key_value = static::database()->quote($this->$primary_key_field());
$conditions = $this->association_foreign_key($association_name) . ' = ' . $primary_key_value;
$association_model = $this->association_model($association_name);
$find_array = call_user_func("$association_model::find_all",
array('conditions' => $conditions)
);
return $find_array;
}
protected function columnExist($columnName){
return (isset ($this->row[$columnName]));
}
}