如何在Zend Framework 2中使用SQL构建视图助手

My Zend Framework 2 Application has a view whereby I display a log of events, in a simple table format. I use several basic View Helpers to manipulate the presentation of the data in the table, but on these existing View Helpers all of the logic is contained to the View Helper itself, e.g:

namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;

class GetSystemName extends AbstractHelper
{
    public function __invoke($val)
    {
        if ($val == 0){
            return 'Something';
        }
        if ($val == 1){
            return 'Something else';
       }
    }
}

My requirement is to build a function GetUserName to accept user_id and perform a check on the database to display the User's name, as the ID is of no value to the person using the system.

The way I see it I can either:

A) Start a new query from within the View Helper to return what I need or B) Use a function called getUser() from within the 'User' Module / UserTable class.

The code for B is:

namespace User\Model;

use Zend\Db\TableGateway\TableGateway;

 class UserTable
 {
 protected $tableGateway;

 public function __construct(TableGateway $tableGateway)
 {
     $this->tableGateway = $tableGateway;
 }

 //..other functions

 public function getUser($id)
 {
     $id  = (int) $id;
     $rowset = $this->tableGateway->select(array('id' => $id));
     $row = $rowset->current();
     if (!$row) {
         throw new \Exception("Could not find row $id");
     }
     return $row;
 }

What is the best option? And how would I implement it?

Apologies if this is a basic questions I am quite new to MVC and Zend.

In the model-view-controller pattern, the view should not be aware of the model layer. Information from the models are injected into the view through the controller.

Having your view helper call the getUser() method in your model breaks this pattern.

So, what do you do?

Have your controller get the user information into the view:

// controller
$userId = $this->params()->fromQuery("userID"); 
// or from session ID if this is a private profile page
// You might want some validation, too...

$userTable = $this->getServiceLocator()->get("UserTable"); 
// or whatever you've configured in the service config for this

$user = $userTable->getUser($userId); 
// if this is a public profile page, you might want to 
// exclude some fields like "password" so they don't 
// accidentally get into the view

$view = new ViewModel();
$view->setVariable("user", $user);

return $view;

Then in the view.phtml you just do:

<?php $this->GetSystemName($user->whateverField); ?>