1 | <?php |
||
14 | abstract class DataObject |
||
15 | { |
||
16 | /** |
||
17 | * @var int ID of the object |
||
18 | */ |
||
19 | protected $id = 0; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | * TODO: we should probably make this a read-only method rather than public - why should anything external set this? |
||
24 | */ |
||
25 | public $isNew = true; |
||
26 | |||
27 | /** |
||
28 | * @var PdoDatabase |
||
29 | */ |
||
30 | protected $dbObject; |
||
31 | |||
32 | public function setDatabase(PdoDatabase $db) |
||
33 | { |
||
34 | $this->dbObject = $db; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Gets the database associated with this data object. |
||
39 | * @return PdoDatabase |
||
40 | */ |
||
41 | public function getDatabase() |
||
42 | { |
||
43 | return $this->dbObject; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Retrieves a data object by it's row ID. |
||
48 | * @param int $id |
||
49 | * @param PdoDatabase $database |
||
50 | * @return DataObject|null |
||
51 | */ |
||
52 | public static function getById($id, PdoDatabase $database) |
||
53 | { |
||
54 | $statement = $database->prepare("SELECT * FROM `" . strtolower(get_called_class()) . "` WHERE id = :id LIMIT 1;"); |
||
55 | $statement->bindValue(":id", $id); |
||
56 | |||
57 | $statement->execute(); |
||
58 | |||
59 | $resultObject = $statement->fetchObject(get_called_class()); |
||
60 | |||
61 | if ($resultObject != false) { |
||
62 | $resultObject->isNew = false; |
||
63 | $resultObject->setDatabase($database); |
||
64 | } |
||
65 | |||
66 | return $resultObject; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Saves a data object to the database, either updating or inserting a record. |
||
71 | */ |
||
72 | abstract public function save(); |
||
73 | |||
74 | /** |
||
75 | * Retrieves the ID attribute |
||
76 | */ |
||
77 | public function getId() |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Deletes the object from the database |
||
84 | */ |
||
85 | public function delete() |
||
97 | } |
||
98 | |||
99 | public function getObjectDescription() |
||
100 | { |
||
102 | } |
||
103 | } |
||
104 |