<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tim Bowler &#187; mysql</title>
	<atom:link href="http://timothybowler.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://timothybowler.com</link>
	<description>Certified Scrum Master &#124; Certified Scrum Practitioner &#124; Agile Coach &#124; Agile Practitioner</description>
	<lastBuildDate>Sat, 19 Dec 2009 16:14:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Use a join</title>
		<link>http://timothybowler.com/2008/04/30/use-a-join/</link>
		<comments>http://timothybowler.com/2008/04/30/use-a-join/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 15:34:43 +0000</pubDate>
		<dc:creator>Tim Bowler</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Website performance optimization]]></category>
		<category><![CDATA[left join]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[symfony left join]]></category>

		<guid isPermaLink="false">http://timothybowler.com/2008/04/30/use-a-join/</guid>
		<description><![CDATA[I was refactoring a few developers code today, and one thing I see time and time again is two queries in favour of a left join, . The problem is symfony makes result set retrieval so easy (thanks to propel of course).
Example:
To see if an author has a comment


$author = AuthorPeer::retrieveByPK($id);
$authorComments = CommentPeer::getAuthorComments($author-&#38;gt;getAuthorId());

//In the model
public [...]]]></description>
			<content:encoded><![CDATA[<p>I was refactoring a few developers code today, and one thing I see time and time again is two queries in favour of a left join, . The problem is symfony makes result set retrieval so easy (thanks to propel of course).</p>
<p>Example:<br />
To see if an author has a comment<br />
<span id="more-33"></span></p>
<p><pre><pre>
$author = AuthorPeer::retrieveByPK($id);
$authorComments = CommentPeer::getAuthorComments($author-&amp;gt;getAuthorId());

//In the model
public static function getAuthorComments($id)
{

&nbsp;&nbsp;&nbsp;&nbsp;$c = new Criteria();
&nbsp;&nbsp;&nbsp;&nbsp;$c-&amp;gt;add(self::AUTHOR_ID, $id);

&nbsp;&nbsp;&nbsp;&nbsp;return self::doSelect($c);

}</pre></pre><br />
The reason why people would want to use this is so they can see if a comment is available. Which is a fair point if your sql is rusty <img src='http://timothybowler.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
<pre><pre>
$author = AuthorPeer::getAuthorComments($id);
$author = array_pop($author);

...
...

//In the model
public static function getAuthorComments($id)
{
$c = new Criteria();
$c-&amp;gt;add(self::AUTHOR_ID, $id);

$c-&amp;gt;addJoin(self::AUTHOR_ID, CommentPeer::AUTHOR_ID, Criteria::LEFT_JOIN);
$c-&amp;gt;setLimit(1);
return self::doSelect($c);
}</pre></pre><br />
As you can see the latter users a left join, which means only one database query.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Ftimothybowler.com%2F2008%2F04%2F30%2Fuse-a-join%2F';
  addthis_title  = 'Use+a+join';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://timothybowler.com/2008/04/30/use-a-join/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
