-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelation.php
executable file
·130 lines (110 loc) · 2.79 KB
/
relation.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
include_once 'connect.php';
class Relation
{
protected $__tablename__ = null;
protected function __construct(...$kwargs)
{
foreach ($kwargs[0] as $key => $value)
{
$this->_setAttribute($key, $value);
}
}
private function _getAttribute($attributeName)
{
$reflection = new ReflectionClass($this);
if ($reflection->hasProperty($attributeName))
{
$property = $reflection->getProperty($attributeName);
$property->setAccessible(true);
return $property->getValue($this);
}
else
{
return null;
}
}
private function _setAttribute($attributeName, $attributeValue)
{
$reflection = new ReflectionClass($this);
if ($reflection->hasProperty($attributeName))
{
$property = $reflection->getProperty($attributeName);
$property->setAccessible(true);
$property->setValue($this, $attributeValue);
}
else
{
return null;
}
}
public function getAttribute($attributeName)
{
return $this->_getAttribute($attributeName);
}
public function setAttribute($attributeName, $attributeValue)
{
$this->_setAttribute($attributeName, $attributeValue);
}
private function _addToDatabase()
{
// Create a clone of calling object
$reflection = new ReflectionClass($this);
$dsn = 'mysql:host=localhost; dbname=drugs_db';
$username = 'root';
$password = 'MySQLXXX-123a8910';
try
{
$dbHandler = new DatabaseHandler($dsn, $username, $password);
$dbHandler->connect();
$attributes = [];
// Retrieve attributes and their values
foreach ($reflection->getProperties() as $property)
{
$propertyName = $property->getName();
$property->setAccessible(true);
$propertyValue = $property->getValue($this);
# Only for attributes assigned values to them
if (!empty($propertyValue) || $propertyValue === 0)
{
$attributes[$propertyName] = $propertyValue;
}
}
// Remove special properties from the attributes array
unset($attributes['__tablename__']);
// Generate the SQL INSERT query statement
$columns = implode(', ', array_keys($attributes));
$placeholders = ':' . implode(', :', array_keys($attributes));
$SQL = "INSERT INTO {$this->__tablename__} ($columns) VALUES " .
"($placeholders)";
// Execute the query
$success = $dbHandler->executeQuery($SQL, $attributes);
$dbHandler->disconnect();
if ($success)
{
return $success;
}
else
{
$errorInfo = $statement->errorInfo();
$errorMessage = $errorInfo[2];
throw new PDOException($errorMessage);
}
// close the database connection
$pdo = null;
// Operation successfull :)
return $success;
}
catch (PDOException $error)
{
echo "Error occured while saving data: " . $error->getMessage();
// Operation failed :(
return false;
}
}
public function save()
{
return $this->_addToDatabase();
}
}
?>