I start to understand about the Model-View-Controller pattern by now except one thing that I think I am stuck, the Model part of the pattern.
I now have successfully made the Controller and the View work by simply routing the request, grab the parameters and parse them to the proper controller that extends the core functions and generates the view.
Now I don't really know how to make the Model work. As far as I know, Model is just the structure of the data so here I have the file called Article.php which is the model for blog content
Article.php
<?php
class Article extends Database {
public $id;
public $topic;
public $content;
}
?>
This class extends the Database class as shown down here
Database.php
<?php
class Database {
// Current connection which only one connection is allowed in this version
protected $connection;
// MySql Setting Array
protected $dbsettings = array();
public function __construct() {
require_once 'bin/config/db.php';
foreach($db_setting_array as $k => $v) {
$this->dbsettings[$k] = $v;
}
$this->connection = new mysqli(
$this->dbsettings['server'],
$this->dbsettings['username'],
$this->dbsettings['password'],
$this->dbsettings['database']
);
}
public function inject($qr) {
$result = $this->connection->query($qr);
}
public function call($qr) {
$result = $this->connection->query($qr);
return $result;
}
}
?>
and the Article class is loaded by the buildData method from blog.php with Blog class that extends the core Controller
Blog.php
<?php
class blog extends Controller {
protected $article;
public function __construct() {
/* Load data structure of Entry.php */
$this->article = $this->BuildData('Entry');
}
public function all($linkname='LINK BACK HOME') {
$this->loadtemplate('blog/blog', array('linkname'=>$linkname));
}
public function each($blogid = '') {
// Query the database here
$result = $this->article->call('select * from articles where id='.$blogid);
while($rows = $result->fetch_assoc()) {
$data['id'] = $rows['id'];
$data['title'] = $rows['title'];
$data['content'] = $rows['content'];
}
$this->loadtemplate('blog/each', $data);
$query = 'insert into articles(title, content) values (\'test2\', \'test2\')';
$this->article->inject($query);
}
};
?>
You can see that inside the Database.php I declared two methods which are inject and call which are used to query the database and I use this in the Blog.php which extends the core controller.
With all of these codes, I don't have to do anything with the model. I can just use the "inject" and "call" method that I declared to query the database and parse the array of the result to the view as seen in the Blog.php.
So how Model really works and if I want to use it? Or how should it be updated at, the controller or inside the Model class itself?