Actually, I'd probably take that one step further:
// In your controller
$model = new Bug();
$activeBugs = $model->fetchActive();
$this->view->activeBugs = $activeBugs;
// In your view
echo $this->bugList($this->activeBugs);
// View helper
class My_View_Helper_BugList extends Zend_View_Helper_Abstract
{
public function bugList(array $bugs)
{
foreach ($bugs as $bug) {
$bug = (array) $bug;
// ...
}
}
}
That keeps the view helper fully unaware of the model while still getting
the same result. In the future if your Bug model changes or you need to use
different subclasses of Bug in different views, you can just instantiate the
correct object and call the right query on it and then pass the results to
your view.
CM
<http://cmorrell.com/> *Chris Morrell* Web: http://cmorrell.com Twitter:
@inxilpro <http://twitter.com/inxilpro>
On Tue, Mar 30, 2010 at 11:15 PM, Chris Morrell <[email protected]> wrote:
> The example warns about calling action() which has to clone half the
> dispatch process, dispatch a new action, grab the response body and return
> it. Obviously that's a pretty expensive call. That said, I have to agree
> with the commenter. You could get a similar result with:
>
> class My_View_Helper_BugList extends Zend_View_Helper_Abstract
> {
> public function bugList(Bug $model)
> {
> // ...
> }
> }
>
> // In your controller
> $this->view->bug = new Bug();
>
> // In your view
> $this->butList($this->bug);
>
> That way your view (and view helper) only handle the display logic and the
> controller handles passing the model to the view.
>
> CM
>
> <http://cmorrell.com/> *Chris Morrell* Web: http://cmorrell.com Twitter:
> @inxilpro <http://twitter.com/inxilpro>
>
>
> On Tue, Mar 30, 2010 at 6:58 PM, My Lists <[email protected]> wrote:
>
>> Hello people, check out this example found at :
>>
>> http://framework.zend.com/manual/en/performance.view.html
>>
>>
>> class My_View_Helper_BugList extends Zend_View_Helper_Abstract
>> {
>> public function bugList()
>> {
>> $model = new Bug();
>> $html = "<ul>\n";
>> foreach ($model->fetchActive() as $bug) {
>> $html .= sprintf(
>> "<li><b>%s</b>: %s</li>\n",
>> $this->view->escape($bug->id),
>> $this->view->escape($bug->summary)
>> );
>> }
>> $html .= "</ul>\n";
>> return $html;
>> }
>> }
>>
>>
>> I like the approach to attach the Model inside view helper for
>> specific stuff and reusability. Im right now needing a helper exactly
>> like this but i dont know if i'm violating any pattern rule.
>>
>> One guy as commented out about above example that this is a bad
>> practive since the view shouldn't know the model. What do you think
>> about this ? Should i go ahead in the same way?
>>
>> I have read in many places that this is pretty acceptable in the MVC,
>> just wanted to confirm this.
>>
>> Thanks,
>>
>>
>