Model:
class News
{
public $title;
public $article;
public $date;
public $published;
public $publisher;
public $image;
public static function getSingleArticle($id)
{
$query="SELECT *, date_format(date, '%D %M %Y') AS uk_date FROM news WHERE id=$id";
return mysql_query($query);
}
}
Controller:
public function editAction()
{
$row=News::getSingleArticle($_GET['id']);
$model=mysql_fetch_object($row);
include('application/view/admin/includes/header.php');
include('application/view/admin/news/update.php');
include('application/view/admin/includes/footer.php');
}
In my view a have a form that displays the attributes such as <?php echo $model->title; ?>
In my database table, the fields publisher and image are not present, so I am getting an “undefined property: stdClass” error. However I need to keep these fields in the view for future use. How can I bring in the public model variables to be part of the model object whenever I make a call to the model? (in this case I want them to just return a null value)
It’s not the perfect model class, but I think it comes close to yours while offering a more direct interface to it’s values:
Controller:
However you should really look into PDO because it allows to fetch objects from the database more specifically and more streamlined.
Also use parametrized queries because of the SQL injection that is available as a build-in feature to extend the SQL on the fly via the
$idparameter. I assume you don’t want that.