diff options
Implemented update from DB and updated some comments and phpdocumentator tags
3 files changed, 55 insertions, 12 deletions
| -rw-r--r-- | src/index.php | 6 | ||||
| -rw-r--r-- | src/models/DB.php | 40 | ||||
| -rw-r--r-- | src/models/JobOffer.php | 21 |
diff --git a/src/index.php b/src/index.php index 55bba39..6478815 100644 --- a/src/index.php +++ b/src/index.php @@ -11,4 +11,10 @@ $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; ?> diff --git a/src/models/DB.php b/src/models/DB.php index b973e4b..4c75d17 100644 --- a/src/models/DB.php +++ b/src/models/DB.php @@ -24,6 +24,8 @@ class DB { $this->connection->exec("USE " . self::$dbname); } + /* Create */ + function createTable(string $tableName, array $columns) { $query = "CREATE TABLE IF NOT EXISTS {$tableName}( id INT NOT NULL AUTO_INCREMENT,"; @@ -37,6 +39,26 @@ class DB { $this->connection->exec($query); } + function insertValue(string $tableName, ...$values) { + $query = "INSERT INTO {$tableName} VALUES(NULL, "; + + foreach($values as $value) { + if (gettype($value) == "string") { + $query .= "'{$value}', "; + } + else { + $query .= "{$value}, "; + } + } + $query = rtrim($query, ", "); + + $query .= ");"; + + $this->connection->exec($query); + } + + /* Read */ + function getById(string $tableName, int $id) { $sth = $this->connection->prepare("SELECT * FROM `{$tableName}` WHERE id = {$id}"); $sth->execute(); @@ -51,20 +73,18 @@ class DB { return $sth->fetchAll(PDO::FETCH_OBJ); } - function insertValue(string $tableName, ...$values) { - $query = "INSERT INTO {$tableName} VALUES(NULL, "; + /* Update */ - foreach($values as $value) { - if (gettype($value) == "string") { - $query .= "'{$value}', "; - } - else { - $query .= "{$value}, "; - } + function updateById(string $tableName, int $id, ...$values) { + $query = "UPDATE {$tableName} SET "; + + for ($i = 0; $i < sizeof($values); $i += 2) { + $query .= "{$values[$i]} = " . (gettype($values[$i+1]) == "string" ? "'{$values[$i+1]}'" : $values[$i+1]) . ", "; } $query = rtrim($query, ", "); - $query .= ");"; + $query .= " WHERE id = {$id};"; + echo $query; $this->connection->exec($query); } diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php index 3bb1791..2627207 100644 --- a/src/models/JobOffer.php +++ b/src/models/JobOffer.php @@ -19,6 +19,17 @@ class JobOffer extends DBModel { self::$DB->createTable(self::TABLE_NAME, self::TABLE_COLUMNS); } + /* Create */ + + /** + * @param JobOffer $jb + */ + static function insertIntoDB($jb) { + self::$DB->insertValue(self::TABLE_NAME, $jb->title, $jb->description, $jb->company, $jb->salary); + } + + /* Read */ + /** * @return JobOffer */ @@ -33,7 +44,13 @@ class JobOffer extends DBModel { return self::$DB->getAllValues(self::TABLE_NAME); } - static function insertIntoDB(JobOffer $jb) { - self::$DB->insertValue(self::TABLE_NAME, $jb->title, $jb->description, $jb->company, $jb->salary); + /* Update */ + + /** + * @param JobOffer $jb + */ + static function updateFromDB($jb) { + echo "here"; + self::$DB->updateById(self::TABLE_NAME, $jb->id, "title", $jb->title, "description", $jb->description, "company", $jb->company, "salary", $jb->salary); } } |
