1 | <?php |
||
24 | abstract class DataObject |
||
25 | { |
||
26 | /** @var int ID of the object */ |
||
27 | protected $id = null; |
||
28 | /** @var int update version for optimistic locking */ |
||
29 | protected $updateversion = 0; |
||
30 | /** |
||
31 | * @var PdoDatabase |
||
32 | */ |
||
33 | protected $dbObject; |
||
34 | |||
35 | /** |
||
36 | * Retrieves a data object by it's row ID. |
||
37 | * |
||
38 | * @param int $id |
||
39 | * @param PdoDatabase $database |
||
40 | * |
||
41 | * @return DataObject|false |
||
42 | */ |
||
43 | public static function getById($id, PdoDatabase $database) |
||
44 | { |
||
45 | $array = explode('\\', get_called_class()); |
||
46 | $realClassName = strtolower(end($array)); |
||
47 | |||
48 | $statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;"); |
||
49 | $statement->bindValue(":id", $id); |
||
50 | |||
51 | $statement->execute(); |
||
52 | |||
53 | $resultObject = $statement->fetchObject(get_called_class()); |
||
54 | |||
55 | if ($resultObject != false) { |
||
56 | $resultObject->setDatabase($database); |
||
57 | } |
||
58 | |||
59 | return $resultObject; |
||
60 | } |
||
61 | |||
62 | 1 | public function setDatabase(PdoDatabase $db) |
|
63 | { |
||
64 | 1 | $this->dbObject = $db; |
|
65 | 1 | } |
|
66 | |||
67 | /** |
||
68 | * Gets the database associated with this data object. |
||
69 | * @return PdoDatabase |
||
70 | */ |
||
71 | public function getDatabase() |
||
72 | { |
||
73 | return $this->dbObject; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Saves a data object to the database, either updating or inserting a record. |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | abstract public function save(); |
||
82 | |||
83 | /** |
||
84 | * Retrieves the ID attribute |
||
85 | */ |
||
86 | public function getId() |
||
90 | |||
91 | /** |
||
92 | * Deletes the object from the database |
||
93 | */ |
||
94 | public function delete() |
||
117 | |||
118 | /** |
||
119 | * @return int |
||
120 | */ |
||
121 | public function getUpdateVersion() |
||
125 | |||
126 | /** |
||
127 | * Sets the update version. |
||
128 | * |
||
129 | * You should never call this to change the value of the update version. You should only call it when passing user |
||
130 | * input through. |
||
131 | * |
||
132 | * @param int $updateVersion |
||
133 | */ |
||
134 | public function setUpdateVersion($updateVersion) |
||
138 | |||
139 | /** |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function isNew() |
||
146 | } |
||
147 |