MysqlAdapter

select($options)

17: 	function select($options) {
18: 		global $Logger;
19: 		$Logger->log($options['sql']);
20: 		if ($query = mysql_query($options['sql'])) {
21:
22: 		} else {
23: 			$Logger->error(mysql_error());
24: 			exit;
25: 		}
26:
27: 		$arr = array ();
28: 		$inflector = new Inflector;
29: 		$class_name = ucwords($inflector->camelize($inflector->singularize($this->table)));
30:
31: 		$row_counter = 0;
32: 		while ($row = mysql_fetch_object($query)) {
33: 			$obj = new $class_name;
34: 			foreach ($row as $key => $value) {
35: 				$obj-> $key = $value;
36: 			}
37: 			$row_counter++;
38: 			$arr[] = $obj;
39: 		}
40:
41: 		if (isset ($options['type'])) {
42: 			if ($options['type'] == 'array') {
43: 				if (count($arr) > 0) {
44: 					return $arr;
45: 				} else {
46: 					return array ();
47: 				}
48: 			} else {
49: 				if (isset ($obj)) {
50: 					return $obj;
51: 				} else {
52: 					return false;
53: 				}
54: 			}
55: 		} else {
56:
57: 			if ($row_counter > 1) {
58: 				return $arr;
59: 			}
60: 			elseif ($row_counter == 1) {
61: 				return $obj;
62: 			} else {
63: 				return array ();
64: 			}
65: 		}
66: 	}

File: lib/model/MysqlAdapter.php, #line 17

render_options($options)

68: 	function render_options($options) {
69: 		$select = '*';
70:
71: 		if (is_array($options)) {
72:
73: 			if (isset ($options['select'])) {
74: 				$select = $options['select'];
75: 			}
76:
77: 			if (isset ($options['from'])) {
78: 				$this->table = $options['from'];
79: 			}
80:
81: 			if (!isset ($options['type'])) {
82: 				$type = 'object';
83: 			} else {
84: 				$type = $options['type'];
85: 			}
86:
87: 			if (in_array('all', $options)) {
88: 				array_shift($options);
89: 				$type = 'array';
90: 			}
91:
92: 			if (in_array('first', $options)) {
93: 				$type = 'object';
94: 				$options['order'] = $this->table . '.`id` asc';
95: 				$options['limit'] = '1';
96: 				array_shift($options);
97: 			}
98:
99: 			if (in_array('last', $options)) {
100: 				$type = 'object';
101: 				$options['order'] = $this->table . '.`id` desc';
102: 				$options['limit'] = '1';
103: 				array_shift($options);
104: 			}
105:
106: 			if (isset ($options['conditions'])) {
107: 				$options['conditions'] = 'where ' . $options['conditions'] . ' ';
108: 			}
109:
110: 			if (isset ($options['group'])) {
111: 				$options['group'] = 'group by ' . $options['group'];
112: 			}
113:
114: 			if (isset ($options['order'])) {
115: 				$options['order'] = 'order by ' . $options['order'] . ' ';
116: 			}
117:
118: 			if (isset ($options['limit'])) {
119: 				if ($options['limit'] == '1') {
120: 					$type = 'object';
121: 				}
122: 				if (isset ($options['offset'])) {
123: 					$options['limit'] = $options['offset'] . ', ' . $options['limit'];
124: 				}
125: 				$options['limit'] = 'limit ' . $options['limit'] . ' ';
126: 			}
127:
128: 		} else {
129:
130: 			$id = $options;
131: 			$options = array ();
132: 			$type = 'object';
133: 			$options['conditions'] = 'where id = ' . $id;
134: 		}
135:
136: 		$options_for_mysql['sql'] = sprintf($this->sql_tags['select'], $select, $this->table, $this->display_options($options));
137: 		$options_for_mysql = array_merge($options_for_mysql, $options);
138: 		$options_for_mysql['type'] = $type;
139: 		$options_for_mysql['table'] = $this->table;
140: 		return $options_for_mysql;
141: 	}

File: lib/model/MysqlAdapter.php, #line 68

update_model()

143: 	function update_model() {
144: 		global $Logger;
146: 		$updates = '';
147:
148: 		$this->table_info($this->table);
149: 		foreach ($this->_column_types as $column => $type) {
150: 			if (isset ($this-> $column)) {
151: 				if ($column == 'updated_at') {
152: 					$updates .= $this->table . ".`$column` = now(), ";
153: 				} else {
154: 					$updates .= $this->table . ".`$column` = '" . strip_tags(addslashes($this-> $column)) . "', ";
155: 				}
156: 			}
157: 		}
158:
159: 		$updates = rtrim($updates, ', ');
160: 		$updates .= ' where ' . $this->table . '.`id` = ' . $this->id;
161: 		$Logger->log($sql = sprintf($this->sql_tags['update'], $this->table, $updates));
162:
163: 		if (mysql_query($sql)) {
164: 			return $this;
165: 		} else {
166: 			$Logger->error("- " . mysql_error());
167: 			return false;
168: 		}
169: 	}

File: lib/model/MysqlAdapter.php, #line 143

update_model_attributes($params)

171: 	function update_model_attributes($params) {
172: 		global $Logger;
173: 		$updates = '';
174:
175: 		$this->table_info($this->table);
176:
177: 		foreach ($this->_column_types as $column => $type) {
178: 			if (isset ($params-> $column)) {
179: 				$updates .= $this->table . ".`$column` = '" . strip_tags(addslashes($params[$column])) . "', ";
180: 			}
181: 		}
182: 		if (isset ($this->_column_types['updated_at']) && !isset ($params['updated_at'])) {
183: 			$updates .= $this->table . ".`updated_at` = now(), ";
184: 		}
185:
186: 		$updates = rtrim($updates, ', ');
187: 		$updates .= ' where ' . $this->table . '.`id` = ' . $this->id;
188: 		$Logger->log($sql = sprintf($this->sql_tags['update'], $this->table, $updates));
189:
190: 		if (mysql_query($sql)) {
191: 			return $this;
192: 		} else {
193: 			$Logger->error("- " . mysql_error());
194: 			return false;
195: 		}
196:
197: 	}

File: lib/model/MysqlAdapter.php, #line 171

update_all($attributes, $options)

199: 	function update_all($attributes, $options) {
200: 		global $Logger;
201: 		$options = $this->_render_options($options);
202: 		unset ($options['sql']);
203: 		unset ($options['type']);
204: 		unset ($options['table']);
205: 		$conditions = $this->_displayOptions($options);
206: 		$updates = '';
207: 		$this->table_info($this->table);
208: 		foreach ($this->_column_types as $column => $type) {
209: 			if (isset ($attributes-> $column)) {
210: 				$updates .= $this->table . ".`$column` = '" . strip_tags(addslashes($attributes[$column])) . "', ";
211: 			}
212: 		}
213: 		if (isset ($this->_column_types['updated_at']) and !isset ($attributes->updated_at)) {
214: 			$updates .= $this->table . ".`updated_at` = now(), ";
215: 		}
216: 		$updates = rtrim($updates, ', ');
217: 		$updates = $updates . " " . $conditions;
218: 		$Logger->log($sql = sprintf($this->sql_tags['update'], $this->table, $updates));
219:
220: 		if (mysql_query($sql)) {
221: 			return $this;
222: 		} else {
223: 			$Logger->error("- " . mysql_error());
224: 			return false;
225: 		}
226: 	}

File: lib/model/MysqlAdapter.php, #line 199

save_model()

228: 	function save_model() {
229: 		global $Logger;
230: 		$columns = '';
231: 		$values = '';
232: 		$this->table_info($this->table);
233: 		foreach ($this->_column_types as $column => $type) {
234: 			if (($column == 'updated_at' && !isset ($this-> $column)) || ($column == 'created_at' && !isset ($this-> $column))) {
235: 				$columns .= $this->table . ".`$column`,";
236: 				$values .= 'now(),';
237: 			}
238: 			if (isset ($this-> $column)) {
239: 				$columns .= $this->table . ".`$column`,";
240: 				$values .= "'" . strip_tags(addslashes($this-> $column)) . "',";
241: 			}
242: 		}
243:
244: 		$Logger->log($sql = sprintf($this->sql_tags['save'], $this->table, rtrim($columns, ','), rtrim($values, ',')));
245:
246: 		if (mysql_query($sql)) {
247: 			$this->id = $this->find_last_id($this->table);
248: 			$this->_save_associations();
249: 			return $this;
250: 		} else {
251: 			$Logger->error("- " . mysql_error());
252: 			return false;
253: 		}
254: 	}

File: lib/model/MysqlAdapter.php, #line 228

destroy_model()

256: 	function destroy_model() {
257: 		global $Logger;
258: 		Callbacks :: callback('before_destroy');
259: 		$destroy = 'where ' . $this->table . '.`id` = ' . $this->id;
260: 		$Logger->log($sql = sprintf($this->sql_tags['delete'], $this->table, $destroy));
261: 		return mysql_query($sql);
262: 	}

File: lib/model/MysqlAdapter.php, #line 256

destroy_has_and_belongs_to_many()

264: 	function destroy_has_and_belongs_to_many() {
265: 		if (isset ($this->has_and_belongs_to_many)) {
266: 			global $Logger;
267: 			$inflector = new Inflector;
268: 			foreach ($this->has_and_belongs_to_many as $association_table => $options) {
269: 				if (!isset ($options['foreign_key'])) {
270: 					$options['foreign_key'] = $inflector->singularize($this->table) . '_id';
271: 				}
272: 				$table = Associations :: order_table_association($this->table, $association_table);
273: 				$destroy = "where {$table}.`{$options['foreign_key']}` = {$this->id}";
274: 				$Logger->log($sql = sprintf($this->sql_tags['delete'], $table, $destroy));
275: 				mysql_query($sql);
276: 			}
277: 		}
278: 	}

File: lib/model/MysqlAdapter.php, #line 264

destroy_polymorphic()

280: 	function destroy_polymorphic() {
281: 		if (isset ($this->has_many)) {
282: 			$inflector = new Inflector;
283: 			global $Logger;
284: 			foreach ($this->has_many as $association_table => $options) {
285: 				if (isset ($options['dependent'])) {
286: 					if ($options['dependent'] == 'destroy') {
287: 						$type = $inflector->singularize($inflector->camelize($this->table));
288: 						$destroy = 'where ' . $options['as'] . '_type = '' . $type . '' and ' . $options['as'] . '_id = ' . $this->id;
289: 						$Logger->log($sql = sprintf($this->sql_tags['delete'], $association_table, $destroy));
290: 						return mysql_query($sql);
291: 					}
292: 				}
293: 			}
294: 		}
295: 	}

File: lib/model/MysqlAdapter.php, #line 280

include_models()

297: 	function include_models() {
298: 		if ($models = opendir(DIR_MODELS)) {
299: 			$inflector = new Inflector;
300: 			while (false !== ($file = readdir($models))) {
301: 				if (preg_match('/.php/', $file)) {
302: 					$class_name = $inflector->camelize(str_replace('.php', '', $file));
303: 					include_once DIR_MODELS . $file;
304: 					$this-> $class_name = new $class_name;
305: 					$this-> $class_name->params = & $this->params;
306: 				}
307: 			}
308: 			closedir($models);
309: 		}
310: 	}

File: lib/model/MysqlAdapter.php, #line 297

set_field_error($field_name, $message)

312: 	function set_field_error($field_name, $message) {
313: 		$set_field_error[$field_name] = $message;
314: 		$this->field_error = array_merge($this->field_error, $set_field_error);
315: 	}

File: lib/model/MysqlAdapter.php, #line 312

field_error($field_name)

317: 	function field_error($field_name) {
318: 		if (isset ($this->field_error[$field_name])) {
319: 			return $this->field_error[$field_name];
320: 		}
321: 	}

File: lib/model/MysqlAdapter.php, #line 317

_find_by($method, $args)

323: 	function _find_by($method, $args) {
324: 		$options = $args['conditions'];
325:
326: 		$col = preg_replace('/find_by_|find_all_by_/', '', $method);
327: 		if (preg_match('/and/', $method)) {
328: 			$col = explode('_and_', $col);
329: 			$cond = '';
330: 			foreach ($col as $key => $column) {
331: 				$cond .= $this->table . ".`$column` = '" . $args[$key] . "' and ";
332: 			}
333: 			$cond = rtrim($cond, ' and ');
334: 		} else {
335: 			$cond = $this->table . ".`$col` = '" . $args[0] . "'";
336: 		}
337:
338: 		foreach ($args as $arg) {
339:
340: 			if (is_array($arg)) {
341: 				foreach ($arg as $key => $value) {
342: 					$options[$key] = $value;
343: 				}
344: 			}
345: 		}
346:
347: 		if (isset ($options['conditions'])) {
348: 			$options['conditions'] .= " and (" . $cond . ")";
349: 		} else {
350: 			$options['conditions'] = $cond;
351: 		}
352: 		return $this->find($options);
353: 	}

File: lib/model/MysqlAdapter.php, #line 323

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>