fixed error with DatabaseTable

This commit is contained in:
Joshua Perry 2023-01-23 16:08:51 +00:00
parent 2f280f6f46
commit e5a3069ebd
4 changed files with 44 additions and 26 deletions

View File

@ -25,7 +25,7 @@ class DatabaseTable {
$keys = \array_keys($record);
$columns = \implode(', ', $keys);
$values = \implode(', :', $keys);
startDB()->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record);
$this->startDB()->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record);
}
private function update($record) {
@ -34,18 +34,24 @@ class DatabaseTable {
$params[] = $key . ' = :' .$key;
}
$record['primaryKey'] = $record[$this->pk];
startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
$this->startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
}
public function find($column, $value) {
$values = [
'value' => $value
];
return startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $field . ' = :value')->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor)->execute($values)->fetchAll();
$stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value');
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute($values);
return $stmt->fetchAll();
}
public function findAll() {
return startDB()->prepare('SELECT * FROM ' . $this->table)->execute()->fetchAll();
$stmt = $this->startDB()->prepare('SELECT * FROM ' . $this->table);
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute();
return $stmt->fetchAll();
}
public function delete($column, $value) {

View File

@ -3,12 +3,12 @@ namespace jobs;
class Routes implements \CSY2028\Routes {
public function getController($name) {
$catsTable = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category');
$jobsTable = new \CSY2028\DatabaseTable('jobs', 'id', '\jobs\Entity\Job', [$catsTable]);
$jobsTable = new \CSY2028\DatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]);
$appsTable = new \CSY2028\DatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]);
$controllers = [];
//TODO: Add Controllers
$controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable);
$controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable);
return $controllers[$name];
}

View File

@ -1,15 +1,39 @@
<?php
namespace jobs\controllers;
class Jobs {
private $jobTable;
private $jobsTable;
private $catsTable;
public function __construct(\CSY2028\DatabaseTable $jobTable) {
$this->jobTable = $jobTable;
public function __construct(\CSY2028\DatabaseTable $jobsTable, \CSY2028\DatabaseTable $catsTable) {
$this->jobsTable = $jobsTable;
$this->catsTable = $catsTable;
}
public function home() {
return ['template' => 'home.html.php',
'title' => 'Jo\'s Jobs- Home',
'vars' => ['cats' => $this->catsTable->findAll()]
];
}
public function category() {
$cat = $this->catsTable->find('name', $_GET['page']);
if ($cat == null) {
return $this->notFound();
}
else {
return ['template' => 'category.html.php',
'title' => 'Jo\'s Jobs- '. $_GET['page'],
'vars' => ['jobs' => $this->jobsTable->find('categoryId', $cat[0]->id),
'cats' => $this->catsTable->findAll(),
'heading' => $cat[0]->name]
];
}
}
public function notFound() {
return ['template' => 'notFound.html.php',
'title' => 'Jo\'s Jobs- 404 Not Found',
'vars' => []
];
}

View File

@ -35,19 +35,7 @@
<img src="images/randombanner.php"/>
<main class="sidebar">
<section class="left">
<ul>
<li><a href="it.php">IT</a></li>
<li><a href="hr.php">Human Resources</a></li>
<li class="current"><a href="sales.php">Sales</a></li>
</ul>
</section>
<section class="right">
<h1>Sales Jobs</h1>
<ul class="listing">
<?php