title: PHP 5.5 - using yield with PDO transactions, is it possible?
body: <p>I want to make use of generators to handle over chunks of data from a database query while using the minimum ammount of memory at the same time (one of the advantage for generators).</p>
  
  <p>The problem I've found while using generators is associated with this piece of code:</p>
  
  <pre><code>if (!empty($this-&gt;attributes["queries"])) {
      if (!empty($query)) {
          if (!empty($this-&gt;attributes["queries"][$query])) {
              $this-&gt;attributes["queries"][$query]-&gt;output();
          }
      } else {
          foreach ($this-&gt;attributes["queries"] as $query) {
              $query-&gt;output();
          }
      }
  }
  </code></pre>
  
  <p>Optimizations aside, what this code does is:</p>
  
  <ol>
  <li>Checks if we have any allocated SQL query</li>
  <li>Checks if a specific SQL query was given to execute (ignore any other)</li>
  <li>In case a specific SQL query was given and it exists, execute it with <code>output()</code></li>
  <li>If no specific SQL query was given, traverse all SQL queries and execute them with <code>output()</code></li>
  </ol>
  
  <p>The problem is this works flawlessly for any kind of SQL query EXCEPT the most used one: <code>SELECT</code>.</p>
  
  <p>The problem lies within this piece of code:</p>
  
  <pre><code>if (!empty($query)) {
      if (!empty($this-&gt;attributes["queries"][$query])) {
          if ($this-&gt;attributes["queries"][$query]-&gt;attributes["type"] === "SELECT") {
              if ($output = $this-&gt;attributes["link"]-&gt;prepare($this-&gt;attributes["queries"][$query]-&gt;attributes["query"], [\PDO::ATTR_CURSOR =&gt; \PDO::CURSOR_FWDONLY])) {
                  if ($output-&gt;execute(!empty($this-&gt;attributes["queries"][$query]-&gt;attributes["parameters"]) ? $this-&gt;attributes["queries"][$query]-&gt;attributes["parameters"] : null)) {
                      while ($row = $output-&gt;fetch(\PDO::FETCH_ASSOC)) {
                          yield $row;
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This method (called <code>input</code> to differentiate it from the <code>output</code> method with executes any other function) follows the same logic.</p>
  
  <ul>
  <li>Checks if the specified SQL query exists</li>
  <li>Prepares the SQL query specified as a query/parameters pair</li>
  <li>Execute the SQL query and <code>yield</code> the results</li>
  </ul>
  
  <p>I'm a bit lost here so I would like some light on this subject. Seems like yielding any data row makes any other execution branch unusable (I can perform as many <code>SELECT</code> queries as I want, but no transactional operation works (<code>CREATE</code>, <code>INSERT</code>, <code>UPDATE</code>, etc. just isn't issued or executed but not commited).</p>
  
  <p>Can you give me (or help me design) a piece of pseudo code to reflect the correct pattern for this? Even if I code with PHP 5.5 a lot there's still things I don't know or haven't experienced, so a bit of help would be greatly appreciated :)</p>
  
  <p><strong>EDIT:</strong> what I mean with the <code>input()</code> and <code>output()</code> methods is my scheme currently works but <strong>ONLY</strong> if I separate the <code>SELECT</code> query into its own <code>input()</code> method (so using the generator doesn't break the other SQL queries) and leave the <code>output()</code> method for any other SQL operation.</p>