diff --git a/CSY2028/DatabaseTable.php b/CSY2028/DatabaseTable.php index f50782c..9317a05 100644 --- a/CSY2028/DatabaseTable.php +++ b/CSY2028/DatabaseTable.php @@ -1,6 +1,12 @@ pk = $pk; $this->entityClass = $entityClass; $this->entityConstructor = $entityConstructor; - } - - private function startDB() { //TODO: Maybe move - $server = 'mysql'; - $username = 'student'; - $password = 'student'; - $schema = 'job'; - return new \PDO('mysql:dbname='.$schema.';host='.$server, $username, $password); + $this->pdo = new \PDO('mysql:dbname='.$this->schema.';host='.$this->server, $this->username, $this->password); } private function insert($record) { $keys = \array_keys($record); $columns = \implode(', ', $keys); $values = \implode(', :', $keys); - $this->startDB()->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record); + $this->pdo->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record); } private function update($record) { @@ -34,7 +33,7 @@ class DatabaseTable { $params[] = $key . ' = :' .$key; } $record['primaryKey'] = $record[$this->pk]; - $this->startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record); + $this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record); } public function find($column, $value, $column2 = "", $value2 = "") { @@ -42,7 +41,7 @@ class DatabaseTable { $values = [ 'value' => $value ]; - $stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value'); + $stmt = $this->pdo->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value'); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->execute($values); return $stmt->fetchAll(); @@ -52,7 +51,7 @@ class DatabaseTable { 'value' => $value, 'value2' => $value2 ]; - $stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value AND '. $column2 .' = :value2'); + $stmt = $this->pdo->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value AND '. $column2 .' = :value2'); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->execute($values); return $stmt->fetchAll(); @@ -60,7 +59,7 @@ class DatabaseTable { } public function findAll() { - $stmt = $this->startDB()->prepare('SELECT * FROM ' . $this->table); + $stmt = $this->pdo->prepare('SELECT * FROM ' . $this->table); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->execute(); return $stmt->fetchAll(); @@ -70,7 +69,7 @@ class DatabaseTable { $values = [ 'value' => $value ]; - $this->startDB()->prepare('DELETE FROM '. $this->table .' WHERE '. $column .' = :value')->execute($values); + $this->pdo->prepare('DELETE FROM '. $this->table .' WHERE '. $column .' = :value')->execute($values); } public function save($record) { diff --git a/jobs/Entity/Applicant.php b/jobs/Entity/Applicant.php index 3eabab5..3a3e02d 100644 --- a/jobs/Entity/Applicant.php +++ b/jobs/Entity/Applicant.php @@ -9,7 +9,7 @@ class Applicant { public $jobId; private $jobsTable; - public function __construct(\CSY2028\DatabaseTable $jobsTable) { + public function __construct(\jobs\JobDatabaseTable $jobsTable) { $this->jobsTable = $jobsTable; } diff --git a/jobs/Entity/Job.php b/jobs/Entity/Job.php index e2a5681..51e163b 100644 --- a/jobs/Entity/Job.php +++ b/jobs/Entity/Job.php @@ -11,7 +11,7 @@ class Job { public $clientId; private $catsTable; - public function __construct(\CSY2028\DatabaseTable $catsTable) { + public function __construct(\jobs\JobDatabaseTable $catsTable) { $this->catsTable = $catsTable; } diff --git a/jobs/JobDatabaseTable.php b/jobs/JobDatabaseTable.php new file mode 100644 index 0000000..e2dc0b7 --- /dev/null +++ b/jobs/JobDatabaseTable.php @@ -0,0 +1,8 @@ + 'response.html.php', 'title' => 'Jo\'s Jobs- 404 Not Found', 'vars' => ['cats' => $cats->findAll(), @@ -57,7 +55,7 @@ class Routes implements \CSY2028\Routes { } public function nav() { - $cats = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category'); + $cats = new \jobs\JobDatabaseTable('category', 'id', '\jobs\Entity\Category'); return ['template' => 'nav.html.php', 'vars' => ['cats' => $cats->findAll()] ]; diff --git a/jobs/controllers/Jobs.php b/jobs/controllers/Jobs.php index 73b6ac5..1d2f34f 100644 --- a/jobs/controllers/Jobs.php +++ b/jobs/controllers/Jobs.php @@ -6,7 +6,7 @@ class Jobs { private $appsTable; private $vars = []; - public function __construct(\CSY2028\DatabaseTable $jobsTable, \CSY2028\DatabaseTable $catsTable, \CSY2028\DatabaseTable $appsTable) { + public function __construct(\jobs\JobDatabaseTable $jobsTable, \jobs\JobDatabaseTable $catsTable, \jobs\JobDatabaseTable $appsTable) { $this->jobsTable = $jobsTable; $this->catsTable = $catsTable; $this->appsTable = $appsTable; diff --git a/jobs/controllers/Portal.php b/jobs/controllers/Portal.php index f3e0215..8c41182 100644 --- a/jobs/controllers/Portal.php +++ b/jobs/controllers/Portal.php @@ -6,7 +6,7 @@ class Portal { private $appsTable; private $vars; - public function __construct(\CSY2028\DatabaseTable $catsTable, \CSY2028\DatabaseTable $jobsTable, \CSY2028\DatabaseTable $appsTable) { + public function __construct(\jobs\JobDatabaseTable $catsTable, \jobs\JobDatabaseTable $jobsTable, \jobs\JobDatabaseTable $appsTable) { $this->catsTable = $catsTable; $this->jobsTable = $jobsTable; $this->appsTable = $appsTable; diff --git a/jobs/controllers/User.php b/jobs/controllers/User.php index 1821c1c..5bbd46e 100644 --- a/jobs/controllers/User.php +++ b/jobs/controllers/User.php @@ -5,7 +5,7 @@ class User { private $catsTable; private $vars; - public function __construct(\CSY2028\DatabaseTable $usersTable, \CSY2028\DatabaseTable $catsTable) { + public function __construct(\jobs\JobDatabaseTable $usersTable, \jobs\JobDatabaseTable $catsTable) { $this->usersTable = $usersTable; $this->catsTable = $catsTable; $this->vars['cats'] = $this->catsTable->findAll();