Summary Files Commits Branches @commit @diff @stats
path: root / src / models
diff options
context:
space:
mode:
Commit83bb68e766bf12c5c48350bd6d9f8237e301882c (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-04-01 11:22:04 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-04-01 11:22:04 +0300
Filesfd9490a434b8cf7916921280c3903339414192f1 /src/models
devrix-job-board-83bb68e766bf12c5c48350bd6d9f8237e301882c.tar
devrix-job-board-83bb68e766bf12c5c48350bd6d9f8237e301882c.tar.gz
devrix-job-board-83bb68e766bf12c5c48350bd6d9f8237e301882c.zip
Parent852e91a3da52a478c942bf7a664df8d8798b0641 (diff)
Added and improved existing phpdocumentator documentation
3 files changed, 57 insertions, 2 deletions
-rw-r--r--src/models/DB.php39
-rw-r--r--src/models/DBModel.php5
-rw-r--r--src/models/JobOffer.php15
diff --git a/src/models/DB.php b/src/models/DB.php
index b287273..dc69cfe 100644
--- a/src/models/DB.php
+++ b/src/models/DB.php
@@ -1,4 +1,7 @@
<?php
+/**
+ * The model that has the database functionality. DO NOT instantiate this class! If you need an instance, extend the DBModel class!
+ */
class DB {
private static $servername = "localhost";
private static $username = "root";
@@ -12,6 +15,7 @@ class DB {
private PDO $connection;
function __construct() {
+ // Try to connect
try {
$this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
@@ -25,12 +29,21 @@ class DB {
$this->connection->exec($createDBQuery);
- // Make the actual connection to the database
+ // Connect to the actual database
$this->connection->exec("USE " . self::$dbname);
}
/* Create */
+ /**
+ * Creates a table, with the given name and given column information.
+ * **WARNING**: The method also creates an auto-incremented int id column, **DO NOT** specify it!
+ *
+ * @param string[] $columns
+ * Represents the column information. Each value must be like: "column_name data_type", where the data_type should be taken
+ * from the constant values of DB class.
+ * Example value: array("first_name " . DB::STRING_TYPE, "age " . DB::INTEGER_TYPE, "income " . DB::FLOAT_TYPE)
+ */
function createTable(string $tableName, array $columns) {
$query = "CREATE TABLE IF NOT EXISTS {$tableName}(
id INT NOT NULL AUTO_INCREMENT,";
@@ -49,6 +62,14 @@ class DB {
}
}
+ /**
+ * Inserts a value in a given table.
+ *
+ * @param string[] $values
+ * Represents the data that should be inserted. The values should be ordered the same way the table columns are ordered.
+ * Example: "John", 20, 2000.3
+ * **WARNING**: DO NOT insert the id value
+ */
function insertValue(string $tableName, ...$values) {
$query = "INSERT INTO {$tableName} VALUES(NULL, ";
@@ -74,6 +95,9 @@ class DB {
/* Read */
+ /**
+ * Returns the next value that has the given id.
+ */
function getById(string $tableName, int $id) {
$sth = $this->connection->prepare("SELECT * FROM `{$tableName}` WHERE id = {$id}");
$sth->execute();
@@ -81,6 +105,9 @@ class DB {
return $sth->fetch(PDO::FETCH_OBJ);
}
+ /**
+ * Returns all values inside a table.
+ */
function getAllValues(string $tableName) {
$sth = $this->connection->prepare("SELECT * FROM `{$tableName}`");
$sth->execute();
@@ -90,6 +117,13 @@ class DB {
/* Update */
+ /**
+ * Updates the value with the given id inside the specified table.
+ *
+ * @param string[] $values
+ * Represents the data that should be updated. There should always be an even amount of data, where you alternate the column name and column value.
+ * Example: "first_name", "John", "age", 20, "income", 2000.3
+ */
function updateById(string $tableName, int $id, ...$values) {
$query = "UPDATE {$tableName} SET ";
@@ -110,6 +144,9 @@ class DB {
/* Delete */
+ /**
+ * Deletes the value with the given id inside the given table.
+ */
function deleteById(string $tableName, int $id) {
try {
$this->connection->exec("DELETE FROM {$tableName} WHERE id = {$id}");
diff --git a/src/models/DBModel.php b/src/models/DBModel.php
index 859ea97..4e10c18 100644
--- a/src/models/DBModel.php
+++ b/src/models/DBModel.php
@@ -1,9 +1,12 @@
<?php
include_once("DB.php");
+/**
+ * A class, mainly for holding the static DB class intance. You can also get your model id from here.
+ */
abstract class DBModel {
/**
- * Used ONLY for gotten values form the database. Mustn't be assigned to, since it's AUTO_INCREMENT.
+ * Used ONLY for gotten values form the database. Must not be assigned to, since it is AUTO_INCREMENT.
* The id column is set by default in the DB class.
*/
public int $id;
diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php
index 5d89048..4a53b8d 100644
--- a/src/models/JobOffer.php
+++ b/src/models/JobOffer.php
@@ -1,6 +1,9 @@
<?php
include_once("DBModel.php");
+/**
+ * Model that contains the required JobOffer properties. It also contains the request methods to the job_offers table.
+ */
class JobOffer extends DBModel {
public const TABLE_NAME = "job_offers";
public const TABLE_COLUMNS = array("title " . DB::STRING_TYPE, "description " . DB::STRING_TYPE, "company " . DB::STRING_TYPE, "salary " . DB::FLOAT_TYPE);
@@ -22,6 +25,8 @@ class JobOffer extends DBModel {
/* Create */
/**
+ * Inserts the giveb JobOffer into the databse. The id is automatically generated in the database.
+ *
* @param JobOffer $jb
*/
static function insertIntoDB($jb) {
@@ -31,6 +36,8 @@ class JobOffer extends DBModel {
/* Read */
/**
+ * Returns the JobOffer with the given id from the database.
+ *
* @return JobOffer
*/
static function getFromDBById(int $id) {
@@ -38,6 +45,8 @@ class JobOffer extends DBModel {
}
/**
+ * Returns all JobOffers from the database.
+ *
* @return JobOffer[]
*/
static function getAllFromDB() {
@@ -47,6 +56,9 @@ class JobOffer extends DBModel {
/* Update */
/**
+ * Updates the given JobOffer from the database.
+ * **WARNING**: the JobOffer **MUST** have it's database id.
+ *
* @param JobOffer $jb
*/
static function updateFromDB($jb) {
@@ -55,6 +67,9 @@ class JobOffer extends DBModel {
/* Delete */
+ /**
+ * Deletes the JobOffer with the given id from the database.
+ */
static function deleteFromDB(int $id) {
self::$DB->deleteById(self::TABLE_NAME, $id);
}