title: How to run MySQL query with transaction in Zend Framework 2
body: <p>I have zf2 model. Inside the model, there is a function to communicate with the database with transaction. 
  Between the transaction I'm calling a recursive function to do transaction, where I'm passing the transaction object. Without recursive function calling it's running fine, but when I'm going to call any function where some query is running, it sends false, and due to receiving false it's being rollbacked. Below are the example code:</p>
  
  <pre><code>&lt;?php
  
  namespace Admin\Model;
  
  use Zend\Db\TableGateway\TableGateway;
  use Exception;
  
  class ProjectAssignTable {
  
      protected $tableGateway;
  
      public function __construct(TableGateway $tableGateway) {
          $this-&gt;tableGateway = $tableGateway;
      }
  
      /**
       * $empidArray = array(1, 2, 3, 5, 12, 35);
       */
      public function saveProjectAssignment($project_id, $empidArray, $data) {
          $connection = $this-&gt;tableGateway-&gt;getAdapter()-&gt;getDriver()-&gt;getConnection();
          $connection-&gt;beginTransaction();
          try {
              $sqlDelete = "DELETE  FROM rjs_assignproject WHERE project_id= '" . $project_id . "' ";
              $connection-&gt;execute($sqlDelete);
              for ($x = 0; $x &lt; count($empidArray); $x++) {
                  $sqlInsert = "INSERT INTO `rjs_assignproject` SET project_id='" . $project_id . "',admin_id='" . $empidArray[$x] . "',designation_id='" . $data['desgi_' . $empidArray[$x]] . "',comment='" . $data['comment_' . $empidArray[$x]] . "',datetime_from='" . $data['time_from' . $empidArray[$x]] . "',datetime_to='" . $data['time_to' . $empidArray[$x]] . "' ";
                  $resultHistory = $connection-&gt;execute($sqlInsert);
                  if (!$this-&gt;recursiveEntry($connection, $empidArray[$x], $project_id)) {
                      throw new Exception();
                  } else {
                      return true;
                  }
              }
              $connection-&gt;commit();
              return true;
          } catch (Exception $ex) {
              $connection-&gt;rollback();
          }
      }
  
      public function recursiveEntry($connection, $admin_id, $project_id) {
          try {
              $sql1 = "select * from rjs_admins where admin_id in(select parent_id from rjs_admins where admin_id=$admin_id)";
              $result1 = $connection-&gt;execute($sql1);
              if (count($result1) &gt; 0) {
                  $sql2 = "select * from rjs_assignproject where admin_id='" . $result1[0]-&gt;admin_id . "'";
                  $result2 = $connection-&gt;execute($sql2);
                  if (count($result2) &lt; 1) {
                      $sql3 = "INSERT INTO `rjs_assignproject` SET project_id='" . $project_id . "',admin_id='" . $result1[0]-&gt;admin_id . "',designation_id='" . $result1[0]-&gt;designation_id . "' ";
                      $connection-&gt;execute($sql3);
                      if (!$this-&gt;recursiveEntry($connection, $result1[0]-&gt;admin_id, $project_id)) {
                          throw new Exception();
                      } else {
                          return true;
                      }
                  }
              }
          } catch (Exception $ex) {
              return false;
          }
      }
  
  }
  </code></pre>