<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Raw SQL from Doctrine Query Object &#8211; Revised</title>
	<atom:link href="http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/</link>
	<description>A day in the life of...</description>
	<lastBuildDate>Tue, 10 Jan 2012 15:00:09 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
	<item>
		<title>By: Jayden</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-5050</link>
		<dc:creator>Jayden</dc:creator>
		<pubDate>Sat, 05 Nov 2011 23:42:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-5050</guid>
		<description>Hi Christopher, thanks for sharing this. Helped me solved the cockups with my SQL.</description>
		<content:encoded><![CDATA[<p>Hi Christopher, thanks for sharing this. Helped me solved the cockups with my SQL.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Debra</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-4773</link>
		<dc:creator>Debra</dc:creator>
		<pubDate>Wed, 20 Apr 2011 18:55:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-4773</guid>
		<description>At last, somoene comes up with the &quot;right&quot; answer!</description>
		<content:encoded><![CDATA[<p>At last, somoene comes up with the &#8220;right&#8221; answer!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jorge Loria</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-4415</link>
		<dc:creator>Jorge Loria</dc:creator>
		<pubDate>Mon, 14 Feb 2011 07:05:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-4415</guid>
		<description>Excellent!! works like a charm!!
thanks!!!</description>
		<content:encoded><![CDATA[<p>Excellent!! works like a charm!!<br />
thanks!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Shennan</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-3767</link>
		<dc:creator>Christopher Shennan</dc:creator>
		<pubDate>Thu, 04 Nov 2010 17:56:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-3767</guid>
		<description>Hi Japes,

The example I&#039;ve used already incorporates the $q-&gt;getSqlQuery().

getSqlQuery() works out the box with no problems for only the most basic queries, but as soon as you start parameterising your queries you have to substitute all the ? with the values you need.  i.e.  If you write

&lt;pre lang=&quot;php&quot;&gt;
$query = Doctrine::getTable(&#039;News&#039;)-&gt;createQuery();
echo $query-&gt;getSqlQuery();
&lt;/pre&gt;
then you will get:

&lt;strong&gt;SELECT id, title, description FROM news&lt;/strong&gt;

However if you write:-
&lt;pre lang=&quot;php&quot;&gt;
$query = Doctrine::getTable(&#039;News&#039;)-&gt;createQuery()-&gt;addWhere(&#039;is_active = ?&#039;, true);
echo $query-&gt;getSqlQuery();
&lt;/pre&gt;
Then you get:-

&lt;strong&gt;SELECT id, title, description FROM news WHERE (is_active = ?)&lt;/strong&gt;

This not ideal as you cannot simply cut and paste the SQL to debug it and substitution is a pain especially if you have a lot of parameters and are repeatedly testing the query as you have to make the substitutions each time.

This article was written to give you an easy way to substitute all the ? with their appropriate values to allow you to copy and paste the SQL directly for easy debugging.

Hope this helps clear things up.

Chris</description>
		<content:encoded><![CDATA[<p>Hi Japes,</p>
<p>The example I&#8217;ve used already incorporates the $q->getSqlQuery().</p>
<p>getSqlQuery() works out the box with no problems for only the most basic queries, but as soon as you start parameterising your queries you have to substitute all the ? with the values you need.  i.e.  If you write</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'News'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSqlQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>then you will get:</p>
<p><strong>SELECT id, title, description FROM news</strong></p>
<p>However if you write:-</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> Doctrine<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'News'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addWhere</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'is_active = ?'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSqlQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then you get:-</p>
<p><strong>SELECT id, title, description FROM news WHERE (is_active = ?)</strong></p>
<p>This not ideal as you cannot simply cut and paste the SQL to debug it and substitution is a pain especially if you have a lot of parameters and are repeatedly testing the query as you have to make the substitutions each time.</p>
<p>This article was written to give you an easy way to substitute all the ? with their appropriate values to allow you to copy and paste the SQL directly for easy debugging.</p>
<p>Hope this helps clear things up.</p>
<p>Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Japes</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-3766</link>
		<dc:creator>Japes</dc:creator>
		<pubDate>Thu, 04 Nov 2010 16:28:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-3766</guid>
		<description>um, unless I&#039;ve misunderstood the question, Doctrine has that function:
$q-&gt;getSqlQuery();
It&#039;s frequently in the docs to expose the underlying SQL generated by the DQL query object, and its output can be cut and pasted into sql admin. Hope this helps!</description>
		<content:encoded><![CDATA[<p>um, unless I&#8217;ve misunderstood the question, Doctrine has that function:<br />
$q-&gt;getSqlQuery();<br />
It&#8217;s frequently in the docs to expose the underlying SQL generated by the DQL query object, and its output can be cut and pasted into sql admin. Hope this helps!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Shennan</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-1671</link>
		<dc:creator>Christopher Shennan</dc:creator>
		<pubDate>Tue, 16 Feb 2010 14:10:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-1671</guid>
		<description>Thanks for the update Stoyan.

I have now used both variations in my projects now as one was symfony based (so the MyQueryClass solution worked perfectly) and one was not (so the initial global function version worked well in this instance).</description>
		<content:encoded><![CDATA[<p>Thanks for the update Stoyan.</p>
<p>I have now used both variations in my projects now as one was symfony based (so the MyQueryClass solution worked perfectly) and one was not (so the initial global function version worked well in this instance).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stoyan</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-1633</link>
		<dc:creator>Stoyan</dc:creator>
		<pubDate>Tue, 09 Feb 2010 19:08:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-1633</guid>
		<description>Also, for symfony:
1. create /yourproject/lib/MyQueryClass.php:

&lt;pre lang=&quot;php&quot;&gt;
class MyQueryClass extends Doctrine_Query
{
    public function getRawSql()
    {
        $query = $this-&gt;getSqlQuery();

        foreach ($this-&gt;getFlattenedParams() as $param) {
            $query = join(var_export(is_scalar($param) ? $param : (string) $param, true), explode(&#039;?&#039;, $query, 2));
        }

        return $query;
    }
}
&lt;/pre&gt;

Then in yourproject/config/ProjectConfiguration.class.php add the following method:

&lt;pre lang=&quot;php&quot;&gt;
  public function configureDoctrine(Doctrine_Manager $manager)
  {
    $manager-&gt;setAttribute(Doctrine_Core::ATTR_QUERY_CLASS, &#039;MyQueryClass&#039;);
  }
&lt;/pre&gt;

Now you can debug your queries everywhere in the project:

&lt;pre lang=&quot;php&quot;&gt;
    $query-&gt;getRawSql();
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Also, for symfony:<br />
1. create /yourproject/lib/MyQueryClass.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyQueryClass <span style="color: #000000; font-weight: bold;">extends</span> Doctrine_Query
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRawSql<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSqlQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFlattenedParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_scalar</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$param</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$param</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then in yourproject/config/ProjectConfiguration.class.php add the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configureDoctrine<span style="color: #009900;">&#40;</span>Doctrine_Manager <span style="color: #000088;">$manager</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span>Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">ATTR_QUERY_CLASS</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'MyQueryClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you can debug your queries everywhere in the project:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRawSql</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Stoyan</title>
		<link>http://www.chrisshennan.com/2009/12/07/raw-sql-from-doctrine-query-object-revised/comment-page-1/#comment-1632</link>
		<dc:creator>Stoyan</dc:creator>
		<pubDate>Tue, 09 Feb 2010 19:03:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.chrisshennan.com/?p=232#comment-1632</guid>
		<description>I&#039;d propose the following solution:
&lt;pre lang=&quot;php&quot;&gt;
function getRawSqlQuery(Doctrine_Query $q)
{
    $queryString = $query-&gt;getSqlQuery();

    foreach ($query-&gt;getFlattenedParams() as $param) {
        $queryString = join(var_export(is_scalar($param) ? $param : (string) $param, true), explode(&#039;?&#039;, $queryString, 2));
    }

    return $queryString;
}
&lt;/pre&gt;

This is more generic solution, as it respects all param sections (having, set,  join, where). Your solutions works only with the &quot;where&quot; part.</description>
		<content:encoded><![CDATA[<p>I&#8217;d propose the following solution:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getRawSqlQuery<span style="color: #009900;">&#40;</span>Doctrine_Query <span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSqlQuery</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFlattenedParams</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_scalar</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$param</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$param</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'?'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$queryString</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$queryString</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is more generic solution, as it respects all param sections (having, set,  join, where). Your solutions works only with the &#8220;where&#8221; part.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

