]> git.proxmox.com Git - extjs.git/blame - extjs/build/examples/classic/restful/remote/lib/model.php
add extjs 6.0.1 sources
[extjs.git] / extjs / build / examples / classic / restful / remote / lib / model.php
CommitLineData
6527f429
DM
1<?php\r
2/**\r
3 * @class Model\r
4 * Baseclass for Models in this imaginary ORM\r
5 */\r
6class Model {\r
7 public $id, $attributes;\r
8 static function create($params) {\r
9 $obj = new self(get_object_vars($params));\r
10 $obj->save();\r
11 return $obj;\r
12 }\r
13 static function find($id) {\r
14 global $dbh;\r
15 $found = null;\r
16 foreach ($dbh->rs() as $rec) {\r
17 if ($rec['id'] == $id) {\r
18 $found = new self($rec);\r
19 break;\r
20 }\r
21 }\r
22 return $found;\r
23 }\r
24 static function update($id, $params) {\r
25 global $dbh;\r
26 $rec = self::find($id);\r
27\r
28 if ($rec == null) {\r
29 return $rec;\r
30 }\r
31 $rs = $dbh->rs();\r
32\r
33 foreach ($rs as $idx => $row) {\r
34 if ($row['id'] == $id) {\r
35 $rec->attributes = array_merge($rec->attributes, get_object_vars($params));\r
36 $dbh->update($idx, $rec->attributes);\r
37 break;\r
38 }\r
39 }\r
40 return $rec;\r
41 }\r
42 static function destroy($id) {\r
43 global $dbh;\r
44 $rec = null;\r
45 $rs = $dbh->rs();\r
46 foreach ($rs as $idx => $row) {\r
47 if ($row['id'] == $id) {\r
48 $rec = new self($dbh->destroy($idx));\r
49 break;\r
50 }\r
51 }\r
52 return $rec;\r
53 }\r
54 static function all() {\r
55 global $dbh;\r
56 return $dbh->rs();\r
57 }\r
58\r
59 public function __construct($params) {\r
60 $this->id = isset($params['id']) ? $params['id'] : null;\r
61 $this->attributes = $params;\r
62 }\r
63 public function save() {\r
64 global $dbh;\r
65 $this->attributes['id'] = $dbh->pk();\r
66 $dbh->insert($this->attributes);\r
67 }\r
68 public function to_hash() {\r
69 return $this->attributes;\r
70 }\r
71}\r
72\r