diff options
Implemented getById from DB and changed some method names
3 files changed, 22 insertions, 4 deletions
| -rw-r--r-- | src/index.php | 8 | ||||
| -rw-r--r-- | src/models/DB.php | 7 | ||||
| -rw-r--r-- | src/models/JobOffer.php | 11 |
diff --git a/src/index.php b/src/index.php index ecebcd9..55bba39 100644 --- a/src/index.php +++ b/src/index.php @@ -3,8 +3,12 @@ include_once("models/JobOffer.php"); $jb = new JobOffer("Test", "A test", "TheTest", 11); - JobOffer::insertInDB($jb); - foreach(JobOffer::getAll() as $key => $obj) { + // JobOffer::insertInDB($jb); + foreach(JobOffer::getAllFromDB() as $key => $obj) { echo "{$obj->id} {$obj->title}<br>"; } + echo "<br>"; + + $gottenjb = JobOffer::getFromDBById(1); + echo $gottenjb->title; ?> diff --git a/src/models/DB.php b/src/models/DB.php index 299b05a..b973e4b 100644 --- a/src/models/DB.php +++ b/src/models/DB.php @@ -37,6 +37,13 @@ class DB { $this->connection->exec($query); } + function getById(string $tableName, int $id) { + $sth = $this->connection->prepare("SELECT * FROM `{$tableName}` WHERE id = {$id}"); + $sth->execute(); + + return $sth->fetch(PDO::FETCH_OBJ); + } + function getAllValues(string $tableName) { $sth = $this->connection->prepare("SELECT * FROM `{$tableName}`"); $sth->execute(); diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php index 7603211..3bb1791 100644 --- a/src/models/JobOffer.php +++ b/src/models/JobOffer.php @@ -20,13 +20,20 @@ class JobOffer extends DBModel { } /** + * @return JobOffer + */ + static function getFromDBById(int $id) { + return self::$DB->getById(self::TABLE_NAME, $id); + } + + /** * @return JobOffer[] */ - static function getAll() { + static function getAllFromDB() { return self::$DB->getAllValues(self::TABLE_NAME); } - static function insertInDB(JobOffer $jb) { + static function insertIntoDB(JobOffer $jb) { self::$DB->insertValue(self::TABLE_NAME, $jb->title, $jb->description, $jb->company, $jb->salary); } } |
