title: Pre functions for TableGateway in zend framework 2 (zf2)
body: <p>I am using table gateway in zf2. This is simple model table with following working functions. I can insert or update records but I need to know how can we define functions like beforeInsert or beforeUpdate like we do in doctrine(beforePersist).</p>
  
  <pre><code>namespace Application\Model\AnyModel;
  
  use Zend\Db\TableGateway\TableGateway;
  
  class AnyModelTable {
  
  protected $tableGateway;
  
  public function __construct(TableGateway $tableGateway) {
      $this-&gt;tableGateway = $tableGateway;
  }
  
  public function fetchAll() {
      $resultSet = $this-&gt;tableGateway-&gt;select();
      return $resultSet;
  }
  
  public function getRecord($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 found record with $id");
      }
      return $row;
  }
  
  public function saveRecord(AnyModel $record) {
      $data = array(
          'name' =&gt; $record-&gt;name,
          'description' =&gt; $record-&gt;description,
          'status' =&gt; $record-&gt;status,
          'created_by' =&gt; $record-&gt;created_by,
          'created_on' =&gt; $record-&gt;created_on,
      );
      $id = (int) $record-&gt;id;
      if ($id == 0) {
          $this-&gt;tableGateway-&gt;insert($data);
      } else {
          if ($this-&gt;getUser($id)) {
              $this-&gt;tableGateway-&gt;update($data, array('id' =&gt; $id));
          } else {
              throw new \Exception("Could not update user with id $id");
          }
      }
  }
  
  public function deleteRecord($id) {
      $this-&gt;tableGateway-&gt;delete(array('id' =&gt; $id));
  }
  
  public function getArrayCopy() {
      return get_object_vars($this);
  }
  </code></pre>