Created
May 16, 2023 19:42
-
-
Save gnubyte/674579d7b3b2228d33af29f57feb83e0 to your computer and use it in GitHub Desktop.
Basing models off the DataObject Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//error_reporting(-1); | |
//ini_set('display_errors', 'On'); | |
// config.php contents: | |
//define("DB_DSN", "mysql:dbname=database"); | |
//define("DB_USERNAME", "username"); | |
//define("DB_PASSWORD", "password!"); | |
require_once "config.php"; | |
abstract class DataObject { | |
protected $data = array(); | |
protected $errors = array(); | |
public function __construct( $data ) { | |
foreach ( $data as $key => $value ) { | |
if ( array_key_exists( $key, $this->data ) ) $this->data[$key] = $value; | |
} | |
} | |
public function getValue( $field ) { | |
if ( array_key_exists( $field, $this->data ) ) { | |
return $this->data[$field]; | |
} else { | |
die( "Field not found: " . $field); | |
} | |
} | |
public function setValue( $field, $value ) { | |
if ( array_key_exists( $field, $this->data ) ) { | |
$this->data[$field] = $value; | |
} else { | |
die( "Field not found: " . $field); | |
} | |
} | |
public function getValueEncoded( $field ) { | |
return htmlspecialchars( $this->getValue( $field ) ); | |
} | |
protected function connect() { | |
try { | |
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); | |
$conn->setAttribute( PDO::ATTR_PERSISTENT, true ); | |
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
} catch ( PDOException $e ) { | |
die( "Connection failed: " . $e->getMessage() ); | |
} | |
return $conn; | |
} | |
protected function disconnect( $conn ) { | |
$conn = ""; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "DataObject.class.php"; | |
class Post extends DataObject | |
{ | |
protected $table_name = "posts"; | |
var $data = array( | |
"id" => "", | |
"blogpost" => "", | |
"upload" => "", | |
"createdate" => "", | |
"userid" => "", | |
"title" => "", | |
"tags" => "" | |
); | |
public function __construct(array $args=[]) | |
{ | |
$this->$data["id"] = ""; | |
$this->$data["blogpost"] = ""; | |
$this->$data["upload"] = ""; | |
$this->$data["createdate"] = ""; | |
$this->$data["userid"] = ""; | |
$this->$data["title"] = ""; | |
$this->$data["tags"] = ""; | |
if ($args){ | |
foreach ($arr as $key => $value){ | |
$this->$data[$key] = $value; | |
} | |
} | |
} | |
public function getPost($id) | |
{ | |
$conn = parent::connect(); | |
$sql = "SELECT * FROM posts WHERE id = :id"; | |
try | |
{ | |
$st = $conn->prepare($sql); | |
$st->bindValue(":id", $id, PDO::PARAM_STR); | |
$st->execute(); | |
$row = $st->fetch(); | |
parent::disconnect($conn); | |
if ($row) | |
{ | |
$this->setValue("id", $row['id']); | |
$this->setValue("blogpost", $row['blogpost']); | |
$this->setValue("upload", $row['upload']); | |
$this->setValue("createdate", $row['createdate']); | |
$this->setValue("userid", $row['userid']); | |
$this->setValue("title", $row['title']); | |
$this->setValue("tags", $row['tags']); | |
return $this; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
catch(PDOException $e) | |
{ | |
parent::disconnect($conn); | |
die("Query failed: " . $e->getMessage()); | |
} | |
} //end getUser | |
public static function getAllPosts($limit1, $limit2) | |
{ | |
if(!isset($limit1)){ | |
//show this many | |
$limit1 = "10"; | |
} | |
if(!isset($limit2)){ | |
// start from this record | |
$limit2 = "0"; | |
} | |
$conn = parent::connect(); | |
$sql = "SELECT * FROM posts ORDER BY id DESC"; | |
try | |
{ | |
$st = $conn->prepare($sql); | |
//$st->bindValue(":limit1", $limit1, PDO::PARAM_STR); | |
//$st->bindValue(":limit2", $limit2, PDO::PARAM_STR); | |
$st->execute(); | |
$results = $st->fetchAll(); | |
parent::disconnect($conn); | |
if ($results) | |
{ | |
return $results; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
catch(PDOException $e) | |
{ | |
parent::disconnect($conn); | |
error_log("Query failed: " . $e->getMessage()); | |
return false; | |
} | |
} //end getUser | |
public static function savePost($blogpost, $title, $tags) | |
{ | |
if (!isset($blogpost)){ | |
error_log("Blogpost wasnt set when passed to savePost"); | |
} | |
if (!isset($title)){ | |
error_log("Title wasnt set when passed to savePost"); | |
} | |
if (!isset($tags)){ | |
error_log("Tags wasnt set when passed to savePost"); | |
} | |
$conn = parent::connect(); | |
$sql = "INSERT INTO posts (blogpost, title, tags) VALUES (:blogpost, :title, :tags)"; | |
// Assume that we are using the other methods in this class to build this query | |
try | |
{ | |
$st = $conn->prepare($sql); | |
$st->bindValue(":blogpost", $blogpost); | |
$st->bindValue(":title", $title); | |
$st->bindValue(":tags", $tags); | |
$st->execute(); | |
$id = strval($conn->lastInsertId()); | |
return $id; | |
} //end try for user/email | |
catch(PDOException $e) | |
{ | |
parent::disconnect($conn); | |
//error_log($e->getMessage()); | |
error_log("Query failed: " . $e->getMessage()); | |
return 0; | |
} //end catch for user/email | |
} //end login | |
} //end class | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment