title: Zend Framework 2 Database Access in Models
body: <p>I am developing my app in <code>ZF2</code>  but despite trying i could not find answer for my database/model problem.</p>
  
  <p>I think of TableGateway that is pretty much the copy of skeleton application example as a AdminUser collection of some sorts that does the query tasks and returns AdminUser instance.</p>
  
  <p>this is part of Module.php</p>
  
  <pre><code>public function getServiceConfig()
  {
  
      return array(
          'factories' =&gt; array(
              'Admin\Model\AdminUserTable' =&gt;  function($sm) {
                  $tableGateway = $sm-&gt;get('AdminUserTableGateway');
                  $table = new AdminUserTable($tableGateway);
                  return $table;
              },
              'AdminUserTableGateway' =&gt; function ($sm) {
                  $dbAdapter = $sm-&gt;get('Zend\Db\Adapter\Adapter');
                  $resultSetPrototype = new ResultSet();
                  $resultSetPrototype-&gt;setArrayObjectPrototype(new AdminUser());
                  return new TableGateway('zxf_admin_user', $dbAdapter, null, $resultSetPrototype);
              },
          ),
      );
  }
  </code></pre>
  
  <p>here is the controller</p>
  
  <pre><code>protected $adminUserTable;
  
  public function indexAction()
  {
      $this-&gt;layout('layout/admin');
  
      return new ViewModel(array(
          'text' =&gt; 'some text',
          'users' =&gt; $this-&gt;getAdminUserTable()-&gt;fetchAll(),
      ));
  }
  
  public function getAdminUserTable()
  {
      if (!$this-&gt;adminUserTable) {
          $sm = $this-&gt;getServiceLocator();
          $this-&gt;adminUserTable = $sm-&gt;get('Admin\Model\AdminUserTable');
      }
      return $this-&gt;adminUserTable;
  }
  </code></pre>
  
  <p>I am not sure of the getAdminUserTable approach,and this is the function that gets user</p>
  
  <pre><code>public function getAdminUser($id)
  {
      $id  = (int) $id;
      $rowset = $this-&gt;tableGateway-&gt;select(array('id' =&gt; $id));
      $row = $rowset-&gt;current();
      if (!$row) {
          throw new \Exception("Could not find row $id");
      }
      return $row;
  }
  </code></pre>
  
  <p>but what if I am interested in having another class something like AdminUserPriviliges that would be object that is returned when you do this in your controller</p>
  
  <pre><code>    $user = $this-&gt;getAdminUserTable()-&gt;getAdminUser($id);
  </code></pre>
  
  <p>to get privileges even though they are different class and data for them is stored in different table, hence the below method would need to perform database search but because inside AdminUser object it could use the $this->id and furthermore via AdminUser you would get access to privileges themselves so I could then get them on request as a collection of objects, something like this</p>
  
  <pre><code>    $user_piviliges = $user-&gt;getPriviliges();
  </code></pre>
  
  <p>and the content should be collection of AdminUserPriviliges instances</p>
  
  <pre><code>    return new ViewModel(array(
          'text' =&gt; 'some text',
          'user' =&gt; $this-&gt;getAdminUserTable()-&gt;getAdminUser($id);,
      ));
  </code></pre>
  
  <p>so sending user objects back to view would mean we have the privileges too.
  in a similar way when i move to other entities i would like to be able to do the same</p>
  
  <p>like $product->getImages() would give you all images but they are not stored in the same table of course.</p>
  
  <p>Is there a better way to do it or even simpler?</p>