0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Aduro
  • 47
  • 2
  • 7

3 Answers3

0

Use something like this

$this->set('data_blog', BlogModel::dbBlog());
Nisanth Kumar
  • 5,667
  • 8
  • 33
  • 43
0

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();
    }
}
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
0

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.

Aduro
  • 47
  • 2
  • 7