I'm learning the whole MVC type of programming. I have a MySQl Query inside a model like so:
<?php
class BlogModel extends BaseModel {
public static function dbBlog() {
$db = new Database(true);
$entries = $db->matrix('cms_m1_blog_ml');
$this->set('data_blog', $entries);
}
}
This script worked when I had it in the controller. the view looks like this:
<?php
foreach($data_blog as $blog)
{
echo "<ul>";
echo "<li class='name'>Name:".$blog['title'] . "</li>";
echo "<li class='content'>Content:".$blog['content'] . "</li>";
echo "</ul>";
}
How do I pass the data from the model to the controller? I ave tried all sorts of things but nothing was in the right direction. I use a custom framework build by my employee but it is based on CakePHP.
Use something like this
$this->set('data_blog', BlogModel::dbBlog());
Give the controller a reference to the model. If the model is a static class, simply add a static variable to the controller class, else pass the correct model object as a parameter to the class constructor and store it in a property of the object. Like this:
class BlogModel extends BaseModel {
private $db;
private $entries;
public static function dbBlog() {
$db = new Database(true);
$entries = $db->matrix('cms_m1_blog_ml');
}
// Add getters here
}
class BlogController {
// Get mymodel somehow and store it
public function doStuff() {
$this->mymodel.getDB();
}
}
Found the anwser :)
Controller:
public function blog(){
$this->set('data_blog', BlogModel::dbBlog());
}
public function index() {
}
}
Model:
public static function dbBlog() {
$db = new Database(true);
$entries = $db->matrix('cms_m1_blog_ml');
return $entries;
}
}
So the problem was in the model, I just needed to return the entries and use the set in the controller.