From 3298938d8f3bc7d6dafd8e67311e4ff0afc8d288 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 31 Mar 2021 22:01:00 +0300 Subject: Implemented DBModel for storing an instance of DB --- src/models/DBModel.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/models/DBModel.php (limited to 'src/models/DBModel.php') diff --git a/src/models/DBModel.php b/src/models/DBModel.php new file mode 100644 index 0000000..65bb725 --- /dev/null +++ b/src/models/DBModel.php @@ -0,0 +1,12 @@ + Date: Thu, 1 Apr 2021 09:13:53 +0300 Subject: Updated JobOffer to initialise it's own db table --- src/models/DBModel.php | 2 +- src/models/JobOffer.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'src/models/DBModel.php') diff --git a/src/models/DBModel.php b/src/models/DBModel.php index 65bb725..6980446 100644 --- a/src/models/DBModel.php +++ b/src/models/DBModel.php @@ -2,7 +2,7 @@ include_once("DB.php"); abstract class DBModel { - protected static $DB; + protected static DB $DB; public static function __constructStatic() { self::$DB = new DB(); diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php index d4268db..5cd6de4 100644 --- a/src/models/JobOffer.php +++ b/src/models/JobOffer.php @@ -15,5 +15,7 @@ class JobOffer extends DBModel { $this->description = $description; $this->company = $company; $this->salary = $salary; + + self::$DB->createTable(self::TABLE_NAME, self::TABLE_COLUMNS); } } -- cgit v1.3.1-23-gd82d From 7d845c9e752c92eabf5005f2bd310cf360991c1a Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 1 Apr 2021 09:25:09 +0300 Subject: Added the id property in DBModel --- src/index.php | 2 +- src/models/DBModel.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'src/models/DBModel.php') diff --git a/src/index.php b/src/index.php index 6f290d6..5fefff4 100644 --- a/src/index.php +++ b/src/index.php @@ -4,6 +4,6 @@ $jb = new JobOffer("a", "", "", 0); foreach(JobOffer::getAll() as $key => $obj) { - echo $obj->salary . "
"; + echo $obj->id . "
"; } ?> diff --git a/src/models/DBModel.php b/src/models/DBModel.php index 6980446..859ea97 100644 --- a/src/models/DBModel.php +++ b/src/models/DBModel.php @@ -2,6 +2,12 @@ include_once("DB.php"); abstract class DBModel { + /** + * Used ONLY for gotten values form the database. Mustn't be assigned to, since it's AUTO_INCREMENT. + * The id column is set by default in the DB class. + */ + public int $id; + protected static DB $DB; public static function __constructStatic() { -- cgit v1.3.1-23-gd82d From 83bb68e766bf12c5c48350bd6d9f8237e301882c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 1 Apr 2021 11:22:04 +0300 Subject: Added and improved existing phpdocumentator documentation --- src/index.php | 11 ----------- src/models/DB.php | 39 ++++++++++++++++++++++++++++++++++++++- src/models/DBModel.php | 5 ++++- src/models/JobOffer.php | 15 +++++++++++++++ 4 files changed, 57 insertions(+), 13 deletions(-) (limited to 'src/models/DBModel.php') diff --git a/src/index.php b/src/index.php index 7120cd3..fb5e85e 100644 --- a/src/index.php +++ b/src/index.php @@ -8,15 +8,4 @@ echo "{$obj->id} {$obj->title}
"; } echo "
"; - - $gottenjb = JobOffer::getFromDBById(1); - echo $gottenjb->title; - - $gottenjb->title = "Something even newer"; - echo $gottenjb->title; - JobOffer::updateFromDB($gottenjb); - $secgot = JobOffer::getFromDBById(1); - echo $secgot->title; - - JobOffer::deleteFromDB(3); ?> 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 @@ 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 @@ deleteById(self::TABLE_NAME, $id); } -- cgit v1.3.1-23-gd82d