-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel1.php
More file actions
127 lines (127 loc) · 4 KB
/
Copy pathModel1.php
File metadata and controls
127 lines (127 loc) · 4 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
abstract class Model
{
// @var PDO|null Connection to the database */
protected static $dbc = null;
// @var array Database values for a single record. Array keys should be column names in the DB */
protected $attributes = array();
protected static $table = '';
/**
* Constructor
*
* An instance of a class derived from Model represents a single record in the database.
*
* $param array $attributes Optional array of database values to initialize the model record with
*/
public function __construct(array $attributes = array())
{
self::dbConnect();
// Initialize the $attributes property with the passed value
$this->attributes = $attributes;
}
/**
* Connect to the DB
*
* This method should be called at the beginning of any function that needs to communicate with the database
*/
protected static function dbConnect()
{
if(!self::$dbc)
{
// Connect to database
$dbc = new PDO('mysql:host=127.0.0.1;dbname=users_db', 'admin_user', 'x');
self::$dbc = $dbc;
self::$table = 'users';
// Throw exception on PDO error
self::$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
/**
* Get a value from attributes based on its name
*
* @param string $name key for attributes array
*
* @return mixed|null value from the attributes array or null if it is undefined
*/
public function __get($name)
{
// Return the value from attributes for $name if it exists, else return null
if(array_key_exists($name, $this->attributes))
{
return $this->attributes[$name];
}
else
{
echo "Error: Attribute {$name} does not exist. Returning NULL";
return null;
}
}
/**
* Set a new value for a key in attributes
*
* @param string $name key for attributes array
* @param mixed $value value to be saved in attributes array
*/
public function __set($name, $value)
{
// Store name/value pair in attributes array
$this->attributes[$name] = $value;
}
/** Store the object in the database */
public function save()
{
// Ensure there are values in the attributes array before attempting to save
// Call the proper database method: if the `id` is set this is an update, else it is a insert
if(isset($this->attributes['id']))
{
$this->update();
}
else
{
$this->insert();
}
}
/** Remove the object from the database */
public function delete()
{
// Ensure record exists before attempting to delete
if(isset($this->attributes['id']))
{
self::dbConnect();
$stmt = self::$dbc->query('DELETE FROM users WHERE id = ' . $this->attributes['id']);
echo "Record " . $this->attributes['id'] . " deleted successfully";
}
else
{
echo "Cannot delete: Record does not exist";
}
}
/**
* Find all records in a table
*
* @return User[] Array of instances of the User class with attributes set to values from database
*/
public static function all()
{
// Get connection to the database
self::dbConnect();
// Return all the matching records
// $stmt = self::$dbc->query("SELECT * FROM {static::$table}");
$stmt = self::$dbc->query('SELECT * FROM users');
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
/**
* Insert new entry into database
*
* NOTE: Because this method is abstract, any child class MUST have it defined.
*/
protected abstract function insert();
/**
* Update existing entry in database
*
* NOTE: Because this method is abstract, any child class MUST have it defined.
*/
protected abstract function update();
}
?>