forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.php
105 lines (85 loc) · 2.13 KB
/
Model.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
<?php
namespace Intern;
use \PHPWS_Error;
use \PHPWS_DB;
use \Current_User;
/**
* Model
*
* This is the basic model for actors for an internship.
* Model handles loading/saving the object from the database.
*
* @author Robert Bost <bostrt at tux dot appstate dot edu>
*/
abstract class Model {
public $id;
/** Get an array that's ready to turn into CSV. * */
abstract function getCSV();
/**
* Constructor. Load the model with given ID.
*/
public function __construct($id=0)
{
if ((int) $id > 0) {
$this->id = (int) $id;
$result = $this->load();
if (!$result) {
$this->id = 0;
}
} else {
$this->id = 0;
}
}
public function getId()
{
return $this->id;
}
/**
* Load the model from the database with matching $this->id.
*/
public function load()
{
if (is_null($this->id) || !is_numeric($this->id))
return false;
$db = $this->getDb();
$db->addWhere('id', $this->id);
$result = $db->loadObject($this);
if (PHPWS_Error::logIfError($result)) {
throw new \Exception($result->toString());
}
return $result;
}
/**
* Save model to database
* @return new ID of model.
*/
public function save()
{
$db = $this->getDb();
try {
$result = $db->saveObject($this);
} catch (\Exception $e) {
// rethrow any exceptions
throw $e;
}
if (PHPWS_Error::logIfError($result)) {
throw new \Exception($result->toString());
}
return $this->id;
}
/**
* Delete model from database.
*/
public function delete()
{
if (is_null($this->id) || !is_numeric($this->id))
return false;
$db = $this->getDb();
$db->addWhere('id', $this->id);
$result = $db->delete();
if (PHPWS_Error::logIfError($result)) {
throw new \Exception($result->getMessage(), $result->getCode());
}
return true;
}
}