oopcakephpcakephp-helper

Advice needed in making a CakePHP Helper


I need to make a CakePHP helper which involves making some HTML dynamically. But the part of the code is to make 1-2 database queries. These queries are very essential for this helper as all the text it populates is inside the database.

According to MVC pattern, i should not be making the DB queries in the View (Helper). I am wondering what the best design would be for this case as i want it to align with PHPUnit testing also.

Any ideas would be welcome...


Solution

  • Since the View job is purely to display the (already available) information passed to it from the Controller, I think it would be something like this:

    Your controller:

    public function foo() {
        $bar = $this->MyModel->find('all');
        $this->set(array('bar' => $bar));
    }
    

    Your view:

    $result = $this->MyHelper->foo($bar);