php - Zend Framework: Using controller method in view to call model method -


i've got little problem here. have category_id every product in db. have category table in db categories , id. need put in view together. i've made add, edit , delete action, show action, category showed rest of product description. have problem index action.

in show did this:

public function getproducttable()  {     if (!$this->producttable) {          $sm = $this->getservicelocator();          $this->producttable = $sm->get('product\model\producttable');      }      return $this->producttable;  }  public function getcategorytable() {     if(!$this->categorytable){         $this->categorytable = $this->getservicelocator()             ->get('product\model\categorytable');     }     return $this->categorytable; }   public function showaction()  {     $id = (int) $this->params()->fromroute('id', 0);     if (!$id) {          return $this->redirect()->toroute('product', array(              'action' => 'add'          ));     }      try {          $product = $this->getproducttable()->getproduct($id);          $category = $this->getcategorytable()->getcategory($product->category_id);      }      catch (\exception $ex) {           return $this->redirect()->toroute('product', array(              'action' => 'index'          ));      } 

it's easy, cause during showaction 1 result db, know category_id product has.

but, in index.phtml products db , need iterate them throught foreach. that's place need call

$this->getcategorytable()->getcategory($id); 

since, controller method using sm use model method, how should use in index.phtml view exact category name every product?

it's massively inefficient calling query category names each product individually, instead, write method return array of category names keyed id in categorytable class

public function getcategorynames() {      // query list of names , ids       // return array of category names, keyed id      $categories = array();      foreach ($results $result) {           $categories[$result['id']] = $result['name'];      }      return $categories; } 

call method in controller action , pass result view ...

public function indexaction() {     $categories = $this->getcategorytable()->getcategorynames();     $products = $this->getproducttable()->getproducts();     return new viewmodel(array(         'categories' => $categories,         'products' => $products,     )); } 

in view, can loop on products, , access category name id key in $categories array

// index.phtml <ul> <?php foreach ($products $product) : ?>     <li>product category  name : <?= $categories[$product->category_id]; ?></li> <?php endforeach; ?> </ul> 

the result 2 db calls, instead of 1 call products, , additional call category name each product item individually.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -