refactored db find

This commit is contained in:
Joshua Perry 2023-02-04 22:19:29 +00:00
parent 650e678d9b
commit 136aac803d
6 changed files with 27 additions and 31 deletions

View File

@ -36,27 +36,17 @@ class DatabaseTable {
$this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
}
public function find($column, $value, $column2 = "", $value2 = "", $comparator = "=", $comparator2 = "=", $order = "ASC", $orderColumn = "id") {
if ($column2 == "" || $value2 == "") {
$values = [
'value' => $value
];
$stmt = $this->pdo->prepare('SELECT * FROM '.$this->table.' WHERE '.$column.' '.$comparator.' :value ORDER BY '.$orderColumn." ".$order);
public function find($columns, $values, $comparators = ['=', '='], $order = "ASC", $orderColumn = "id") {
$string = 'SELECT * FROM '.$this->table.' WHERE ';
for ($i = 0; $i < count($values); $i++) {
$string .= $columns[$i].' '.$comparators[$i].' :value'.$i.' ';
}
$string .= 'ORDER BY '.$orderColumn.' '.$order;
$stmt = $this->pdo->prepare($string);
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute($values);
return $stmt->fetchAll();
}
else {
$values = [
'value' => $value,
'value2' => $value2
];
$stmt = $this->pdo->prepare('SELECT * FROM '.$this->table.' WHERE '.$column.' '.$comparator.' :value AND '.$column2.' '.$comparator2.' :value2 ORDER BY '.$orderColumn." ".$order);
$stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
$stmt->execute($values);
return $stmt->fetchAll();
}
}
public function findAll() {
$stmt = $this->pdo->prepare('SELECT * FROM ' . $this->table);

View File

@ -14,7 +14,7 @@ class Applicant {
}
public function getJob() {
return $this->jobsTable->find('id', $this->jobId)[0];
return $this->jobsTable->find(['id'], ['value0' => $this->jobId])[0];
}
}
?>

View File

@ -16,7 +16,7 @@ class Job {
}
public function getCat() {
return $this->catsTable->find('id', $this->categoryId)[0];
return $this->catsTable->find(['id'], ['value0' => $this->categoryId])[0];
}
}
?>

View File

@ -14,7 +14,7 @@ class Jobs {
}
public function home() {
$this->vars['jobs'] = $this->jobsTable->find("closingDate", date("y-m-d"), "", "", ">", "", "DESC", "closingDate");
$this->vars['jobs'] = $this->jobsTable->find(["closingDate"], ['value0' => date('y-m-d')], ['>'], "DESC", "closingDate");
return ['template' => 'home.html.php',
'title' => 'Jo\'s Jobs- Home',
'vars' => $this->vars
@ -22,16 +22,22 @@ class Jobs {
}
public function category() {
$cat = $this->catsTable->find('name', $_GET['page']);
$cat = $this->catsTable->find(['name'], ['value0' => $_GET['page']]);
if ($cat == null) {
return $this->notFound();
}
else {
if (isset($_GET['filter'])) {
$this->vars['jobs'] = $this->jobsTable->find('categoryId', $cat[0]->id, "location", $_GET['filter']);
$columns = ['categoryId', "location", 'closingDate'];
$values = ['value0' => $cat[0]->id,
'value1' => $_GET['filter'],
'value2' => date('y-m-d')
];
$comparators = ["=","=",">"];
$this->vars['jobs'] = $this->jobsTable->find($columns, $values, $comparators);
}
else {
$this->vars['jobs'] = $this->jobsTable->find('categoryId', $cat[0]->id, "closingDate", date("y-m-d"), "=", ">");
$this->vars['jobs'] = $this->jobsTable->find(['categoryId', "closingDate"], ["value0" => $cat[0]->id, "value1" => date("y-m-d")], ["=", ">"]);
}
$this->vars['heading'] = $cat[0]->name;
@ -59,7 +65,7 @@ class Jobs {
}
public function apply() {
$this->vars['job'] = $this->jobsTable->find('id', $_GET['id'])[0];
$this->vars['job'] = $this->jobsTable->find(['id'], ["value0" => $_GET['id']])[0];
return ['template' => 'apply.html.php',
'title' => 'Jo\'s Jobs- Apply',
'vars' => $this->vars];

View File

@ -18,15 +18,15 @@ class Portal {
$this->vars['table'] = 'job_table.html.php';
if (isset($_GET['filter'])) {
if ($_SESSION['userType'] == 'client') {
$this->vars['jobs'] = $this->jobsTable->find('clientId', $_SESSION['loggedin'], "categoryId", $_GET['filter']);
$this->vars['jobs'] = $this->jobsTable->find(['clientId', 'categoryId'], ['value0' => $_SESSION['loggedin'],'value1' => $_GET['filter']]);
}
else {
$this->vars['jobs'] = $this->jobsTable->find("categoryId", $_GET['filter']);
$this->vars['jobs'] = $this->jobsTable->find(["categoryId"], ['value0' => $_GET['filter']]);
}
}
else {
if ($_SESSION['userType'] == 'client') {
$this->vars['jobs'] = $this->jobsTable->find('clientId', $_SESSION['loggedin']);
$this->vars['jobs'] = $this->jobsTable->find(['clientId'], ['value0' => $_SESSION['loggedin']]);
}
else {
$this->vars['jobs'] = $this->jobsTable->findAll();
@ -66,7 +66,7 @@ class Portal {
public function applicants() {
$this->vars['table'] = 'applicant_table.html.php';
$this->vars['apps'] = $this->appsTable->find('jobId', $_GET['app_id']);
$this->vars['apps'] = $this->appsTable->find(['jobId'], ['value0' => $_GET['app_id']]);
return ['template' => 'portal.html.php',
'title' => 'Jo\'s Jobs- Applicants',
'vars' => $this->vars];
@ -74,10 +74,10 @@ class Portal {
public function edit() { //TODO: finish this function
if (isset($_GET['job_id'])) {
$this->vars['job'] = $this->jobsTable->find("id", $_GET['jod_id']);
$this->vars['job'] = $this->jobsTable->find(["id"], ['value0' => $_GET['jod_id']]);
}
if (isset($_GET['cat_id'])) {
$this->vars['cat'] = $this->catsTable->find("id", $_GET['cat_id']);
$this->vars['cat'] = $this->catsTable->find(["id"], ['value0' => $_GET['cat_id']]);
}
}

View File

@ -20,7 +20,7 @@ class User {
public function loginSubmit() {
if ($_POST['username'] != '' && $_POST['password'] != '') {
$user = $this->usersTable->find("username", $_POST['username']);
$user = $this->usersTable->find(["username"], ['value0' => $_POST['username']]);
if (count($user) > 0 && $_POST['submit'] == 'Register') {
$this->vars['response'] = "Account already exists";