MySql query string to sql object
<p>Using Zend Framework 2 is possible to get a sql string from a tablegateway object, like this:</p>

<pre><code>public function test(){
   $sql = $this-&gt;tableGateway-&gt;getSql();
   $select = $sql-&gt;select();
   $select-&gt;where(array('title' =&gt; 'abracadabra'));
   $select-&gt;where
     -&gt;NEST-&gt;
           equalTo('id', 1)
               -&gt;OR-&gt;
           equalTo('artist', 'Tony')
     -&gt;UNNEST;

   $select-&gt;limit(1);

   return $this-&gt;tableGateway-&gt;selectWith($select);
}

//code ignored here//

echo $sql-&gt;getSqlstringForSqlObject($select); 
</code></pre>

<p>I was wondering if it could be possible to do the opposite, and basically set a query string and then get back a tableGateway object, the reason for this is that sometimes is needed to set a where clause dynamically, without having to know where to place/append it, example:</p>

<p>On a single query:</p>

<pre><code>$sql = "select * from tbl_test";

if($isDynamic)
    $sql .= ' where test_id=1';
</code></pre>

<p>But on more complex query:</p>

<pre><code>$sql = "select * from tbl_test inner join (select * from tbl_test 1) x on 1=1 where test_id is not null group by test_id";

//OF COURSE IT WILL FAIL.
if($isDynamic)
    $sql .= ' where test_id=1';
</code></pre>

<p>Using zf2 I could potentially do something like</p>

<ul>
<li>Setting the query string to a sql object.</li>
<li>Then simply do $obj->where(array('test_id'=>1));</li>
</ul>