Before going out and learning a full fledged framework I'm trying to understand the MVC pattern coding basic stuff, at the moment I'm testing with MVC applied to form validation.
After reading a good amount of articles and code examples I came out with my own version, nothing too elaborate as it's mostly for learning, so I need advice in the structure.
For this example I have a basic form which inserts a Category into a database, so I divided it like this:
- class Category extends Operations: This class only deals with database management stuff CRUD operations (the business logic), defines which fields are required and the table name. I call this my model.
- abstract class Operations: Will be extended by other classes that I'll make in the future such as Products, Clients, etc. Contains common properties (id, attributes). Again part of the model.
- class CategoryControl: The controller, deals with $_POST data, setting the corresponding attributes for the Category object and gets its status (insert success/failed, for example).
- abstract class Controller: Generic class which contains form submit status and executes the corresponding CRUD operation.
- Finally the view which is the form, only requesting for the object status to display a sucess/failure message to the user.
Here's the code for each:
view.php
<?php
require('clases/Database.php');
require('clases/CategoryControl.php');
$category = new CategoryControl($database);
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta charset="utf-8" />
</head>
<body>
<form action="view.php" method="post">
<label>Name </label><input type="text" name="name" maxlength="80" /><br />
<label>Parent </label><input type="text" name="parent" maxlength="80" /><br />
<input type="submit" name="submit" value="Insert" />
</form>
<?php
if ($category->getStatus() == 1): ?>
<p>Inserted Sucess</p>
<?php
elseif ($category->getStatus() == 0) : ?>
<p>Null fields</p>
<?php
endif; ?>
</body>
</html>
Category.php
<?php
require("Operations.php");
class Category extends Operations {
const TABLE = 'category';
private $reqFields = array('name', 'parent');
public function __construct($pdo_link) {
parent::__construct($pdo_link);
}
public function insert() {
if ($this->checkNullFields($this->reqFields)) {
$stmt = $this->database->prepare("INSERT INTO ". self::TABLE ." (name, parent) VALUES (:name, :parent)");
foreach ($this->attribs as $field => $value)
$stmt->bindValue(':'.$field, $value);
$stmt->execute();
$stmt = NULL;
return true;
} else
return false;
}
public function update() {
if ($this->checkNullFields($this->reqFields)) {
$stmt = $this->database->prepare("UPDATE ". self::TABLE ." SET name = :name, parent = :parent WHERE id = :id");
foreach ($this->attribs as $field => $value)
$stmt->bindValue(':'.$field, $value);
$stmt->bindValue(':id', $this->id);
$stmt->execute();
$stmt = NULL;
return true;
} else
return false;
}
public function delete() {
if (!empty($this->id)) {
$stmt = $this->database->prepare("DELETE FROM ". self::TABLE ." WHERE id = :id");
$stmt->bindValue(':id', $this->id);
$stmt->execute();
$stmt = NULL;
return true;
} else
return false;
}
}
?>
Operations.php
<?php
abstract class Operations {
protected $database; // PDO link
public $id;
public $attribs;
public $status;
protected function __construct($pdo_link) {
$this->database = $pdo_link;
}
abstract protected function insert();
abstract protected function update();
abstract protected function delete();
protected function checkNullFields($reqFields) {
foreach ($reqFields as $i => $value) {
if (empty($this->attribs[$value]))
return false;
}
return true;
}
}
?>
CategoryControl.php
<?php
require('Category.php');
require('Controller.php');
class CategoryControl extends Controller {
public function __construct($pdo_link) {
$this->model = new Category($pdo_link);
if ($this->checkSubmit()) {
$this->initAttribs();
$this->processPostData();
}
}
public function initAttribs() {
$this->model->id = isset($_GET['id']) ? $_GET['id'] : '';
$this->model->attribs = array(
'name' => isset($_POST['name']) ? $_POST['name'] : '',
'parent' => isset($_POST['parent']) ? $_POST['parent'] : ''
);
}
}
?>
Controller.php
<?php
abstract class Controller {
protected $model;
protected function checkSubmit() {
if (isset($_POST['submit'])) {
return true;
} else {
$this->model->status = -1;
return false;
}
}
protected function processPostData() {
if ($_POST['submit'] == 'Insert')
$this->model->status = $this->model->insert();
else if ($_POST['submit'] == 'Update')
$this->model->status = $this->model->update();
else if ($_POST['submit'] == 'Delete')
$this->model->status = $this->model->delete();
}
abstract protected function initAttribs();
public function getStatus() { return $this->model->status; }
}
?>