title: SELECT problems with Doctrine and MS SQL 2008
body: <p>We have written an application using Doctrine2, which works great when used with a MySQL or Postgres database.</p>
  
  <p>We now connected the application with a SQL Server 2008 database using <a href="http://de3.php.net/manual/en/ref.pdo-dblib.php" rel="nofollow">PDO Driver DBLib</a> which uses the <code>FreeTDS</code> implementation for the TDS (Tabular DataStream protocol), which is shared by SQL Server and Sybase.</p>
  
  <p>The initial schema creation, transactions and <code>INSERT</code>s into the tables work fine .. after some tweaking of the Doctrine <code>SQLServerPlatform</code> and our MSSQL Driver package. </p>
  
  <p>But when selecting data, we face the following error:</p>
  
  <pre><code>General error: 20019 Attempt to initiate a new Adaptive Server operation with results pending
  </code></pre>
  
  <p>After plenty of googling I found <a href="http://www.freetds.org/faq.html#pending" rel="nofollow">this detail in the FAQ of FreeTDS</a>:</p>
  
  <blockquote>
    <p>If you are accustomed to programming with other database servers, you may be surprised when you first encounter this aspect of the TDS protocol.  [...]<br><br>The server requires the client either to read all the results from a query, or to indicate that no further rows are desired i.e., to issue a cancellation. Until one of those two things happens, the server will not accept new queries on that connection. It will complain about "pending results".</p>
  </blockquote>
  
  <p>So the cause for the error message is that for some reason, Doctrine (DBAL->PDO->FreeTDS) did not read all the result rows from the connection buffer, and the server / library doesn't allow the application to issue a new <code>exec()</code>/<code>execute()</code> until all rows are read.</p>
  
  <ul>
  <li>Is my understanding right that <code>$doctrinequery-&gt;getResults()</code> fetches all results before it returns?</li>
  <li>Is my understanding right that <code>$entity-&gt;getLinkedEntity()</code> (i.e. <code>$user-&gt;getGroupNames()</code>) fetches all results before it returns?</li>
  <li>How would you tackle down where exactly the problem is? <br>The stacktraces are not helping us at all and the codebase is already grown.</li>
  <li>Did anybody else already have this problem and can share some insights?</li>
  <li>Is there an alternative we can use to connect Doctrine to MSSQL which does not suffer this problem?</li>
  </ul>