Well I'm not a huge framework guy but I've been liking what I've been hearing about the whole MVC movement so I thought I'd try to create a simple application in my language of choice (PHP)
So I guess the question is: where did I go wrong? I know there is a lot of debate as to how fat the controller/model should be so hopefully we can avoid that, however I am especially curious as to your thoughts on how I fit the datatier in.
Also I bought a domain to do some testing so if you want to see it in action you can go to www.omgmvc.com
Firstly, here's my database schema:
CREATE TABLE `movies` (
`id` int(11) NOT NULL auto_increment,
`movie_name` varchar(255) NOT NULL,
`release_date` date NOT NULL,
`directors_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `movies` VALUES (1,'Star Wars', '1977-05-25', 'George Lucas');
INSERT INTO `movies` VALUES (2,'The Godfather', '1972-03-24', 'Francis Ford Coppola');
INSERT INTO `movies` VALUES (3,'The Dark Knight', '2008-07-18', 'Christopher Nolan');
And here's the files:
index.php (controller)
<?php
include('datatier.php');
include('models/m_movie.php');
if (isset($_GET['movie']) && is_numeric($_GET['movie']))
{
$movie = new Movie($_GET['movie']);
if ($movie->id > 0)
{
include('views/v_movie.php');
}
else
{
echo 'Movie Not Found';
}
}
else
{
$movies = Movie::get_all();
include('views/v_list.php');
}
?>
datatier.php (data tier)
<?php
class DataTier
{
private $database;
function __construct()
{
$this->connect();
}
function __destruct()
{
$this->disconnect();
}
function connect()
{
$this->database = new PDO('mysql:host=localhost;dbname=dbname','username','password');
}
function disconnect()
{
$this->database = null;
}
function get_all_from_database($type)
{
$database = new PDO('mysql:host=localhost;dbname=dbname','username','password');
switch ($type)
{
case 'movie':
$query = 'SELECT id FROM movies';
break;
}
$movies = array();
foreach ($database->query($query) as $results)
{
$movies[sizeof($movies)] = new Movie($results['id']);
}
$database = null;
return $movies;
}
function get_from_database($type,$id)
{
switch ($type)
{
case 'movie':
$query = 'SELECT movie_name,release_date,directors_name FROM movies WHERE id=?';
break;
}
$database_call = $this->database->prepare($query);
$database_call->execute(array($id));
if ($database_call->rowCount() > 0)
{
return $database_call->fetch();
}
else
{
return array();
}
}
}
?>
models/m_movie.php (model)
<?php
class Movie extends DataTier
{
public $id;
public $movie_name;
public $release_date;
public $directors_name;
function __construct($id)
{
parent::connect();
$results = parent::get_from_database('movie',$id);
if ($results == array())
{
$this->id = 0;
}
else
{
$this->id = $id;
$this->movie_name = $results['movie_name'];
$this->release_date = $results['release_date'];
$this->directors_name = $results['directors_name'];
}
}
function __destruct()
{
parent::disconnect();
}
static function get_all()
{
$results = parent::get_all_from_database('movie');
return $results;
}
}
?>
views/v_list.php (view)
<html>
<head>
<title>Movie List</title>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="5">
<thead>
<tr>
<th>Movie Name</th>
<th>Directors Name</th>
<th>Release Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($movies as $movie) { ?>
<tr>
<td><a href="/?movie=<?php echo $movie->id; ?>"><?php echo $movie->movie_name; ?></a></td>
<td><?php echo $movie->directors_name; ?></td>
<td><?php echo $movie->release_date; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
views/v_movie.php (view)
<html>
<head>
<title><?php echo $movie->movie_name; ?></title>
</head>
<body>
<h1><?php echo $movie->movie_name; ?></h1>
<h2>Directed by <?php echo $movie->directors_name; ?></h2>
<h3>Released <?php echo $movie->release_date; ?></h3>
</body>
</html>