I'm new to PHP and OOP and am building methods in order to gather articles from an article database. My method will receive three parameters: $limit
, $category
, $order
. I think this is self-explanatory, however, I have a few questions:
- What happens if one of the arguments is missing? Will the method return false? How can I avoid this?
- I don't know how to gather attributes from two related tables, that is, each article belongs to a category.
- Should I put each of the methods I am building inside a
try
-catch
clause? Will this be beneficial for me or will myif
-else
s do the trick? - Am I doing parameter binding correctly, or should I specify the third argument which I think is the type I should be expecting the variable to be? What if the var introduced is different from the expected type?
Here are my related tables. I think I set up their relationship correctly.
Articles table:
CREATE TABLE articles
(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, -- Each article unique identifier:
categoryId SMALLINT UNSIGNED NOT NULL, -- The artcle category id:
image VARCHAR(255) NULL DEFAULT 'images/img_default_article.jpg', -- Main image of the article:
title TEXT NOT NULL, -- Full title of the article:
summary TEXT NOT NULL, -- The summary of the article:
content MEDIUMTEXT NOT NULL, -- The HTML content of the article:
created DATE NOT NULL, -- Day and Month the article entry was created
modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, --Timestamp the article was last edited
visible INT(11) NOT NULL, -- 1 - Visible, 0 - Not visible:
PRIMARY KEY (id)
FOREIGN KEY (categoryId) REFERENCES categories(id) ON UPDATE CASCADE ON DELETE CASCADE
)
ENGINE=innoDB DEFAULT CHARSET=utf8 COMMENT='all the articles available';
Categories table:
CREATE TABLE categories
(
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, -- Category unique identifier
image TEXT NOT NULL DEFAULT "images/img_default_category.jpg", -- Image of the article
name VARCHAR(255) NOT NULL, -- Name of the category:
description TEXT NOT NULL, -- A short description of the category:
PRIMARY KEY (id)
)
ENGINE=innoDB DEFAULT CHARSET=utf8 COMMENT="Categories for all th articles";
PHP code:
<?php
class Article extends BaseEntity{
private $id, $categoryid, $author, $image, $title, $summary, $content, $created, $modified, $visible;
public function __construct($adapter) {
$table="articles";
parent::__construct($table, $adapter);
}
public function getArticlesAdv($limit, $category, $order){
$sql = "SELECT *
FROM articles
ORDER BY created DESC";
$stmt = $this->db->prepare($sql);
$stmt->execute();
if($stmt->execute()){
$numrows = $stmt->rowCount();
if(numrows>0){
while ($row = $query->fetchAll(PDO::FETCH_OBJ))
{
$resultSet[]=$row;
}
return $resultSet;
}
else
{
return false;
}
}
else
{
return false;
}
}
public function getAllArticles(){
$sql = "SELECT *
FROM articles
ORDER BY created DESC";
$stmt = $this->db->prepare($sql);
$stmt->execute();
$numrows = $stmt->rowCount();
if(numrows>0){
while ($row = $query->fetchAll(PDO::FETCH_OBJ)) {
$resultSet[]=$row;
}
return $resultSet;
}
else{
return false;
}
}
?>