<?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/"
	>

<channel>
	<title>ThinkRobot &#187; Code</title>
	<atom:link href="http://think-robot.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://think-robot.com</link>
	<description>Design &#38; Development Blog</description>
	<pubDate>Sat, 06 Mar 2010 20:58:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multiple changes and a delete on same object in doctrine</title>
		<link>http://think-robot.com/2010/03/multiple-changes-and-a-delete-on-same-object-in-doctrine/</link>
		<comments>http://think-robot.com/2010/03/multiple-changes-and-a-delete-on-same-object-in-doctrine/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 20:58:13 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[doctrine]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[solution]]></category>

		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=219</guid>
		<description><![CDATA[If you want to make several different changes to a doctrine record object you might find yourself slightly puzzled when it comes to deleting related objects.
$user['Address']->delete();
$user['Address']->state(Doctrine_Record::STATE_LOCKED);
(...)
$user->save();
It seems that unless you set the state the save later on still sees an initialized (though empty!) relation.
Related Articles:Doctrine Many To Many With Extra FieldsHitch. Object-oriented event handlers with [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to make several different changes to a doctrine record object you might find yourself slightly puzzled when it comes to deleting related objects.</p>
<pre><code><code>$user['Address']->delete();
$user['Address']->state(Doctrine_Record::STATE_LOCKED);
(...)
$user->save();</code></code></pre>
<p>It seems that unless you set the state the save later on still sees an initialized (though empty!) relation.</p>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/" rel="bookmark">Doctrine Many To Many With Extra Fields</a></li><li><a href="http://think-robot.com/2009/06/hitch-object-oriented-event-handlers-with-jquery/" rel="bookmark">Hitch. Object-oriented event handlers with jQuery</a></li><li><a href="http://think-robot.com/2008/08/server-error-500-htaccess-require-valid-user/" rel="bookmark">Server Error 500 - htaccess require valid-user</a></li><li><a href="http://think-robot.com/2008/06/atmedia-london-2008/" rel="bookmark">@media - London, 2008</a></li><li><a href="http://think-robot.com/2008/08/opensuse-11-on-lenovo-thinkpad-x61-tablet-pc/" rel="bookmark">openSUSE 11 on Lenovo Thinkpad X61 (tablet pc)</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2010/03/multiple-changes-and-a-delete-on-same-object-in-doctrine/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHPUnit &amp; Selenium - screenshot path problem</title>
		<link>http://think-robot.com/2009/12/phpunit-selenium-screenshot-path-problem/</link>
		<comments>http://think-robot.com/2009/12/phpunit-selenium-screenshot-path-problem/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 23:04:46 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[error]]></category>

		<category><![CDATA[path]]></category>

		<category><![CDATA[phpunit]]></category>

		<category><![CDATA[screenshot]]></category>

		<category><![CDATA[selenium]]></category>

		<category><![CDATA[selenium rc]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=207</guid>
		<description><![CDATA[PHPUnit_Framework_Exception: Response from Selenium RC server for testComplete().
ERROR: Command execution failure. Please search the forum at http://clearspace.openqa.org for error details from the log window.
The error message is: Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsILocalFile.initWithPath]

Setting up Selenium RC server together with PHPUnit has proven surprisingly easy&#8230; Well most of it. Every now and then as a [...]]]></description>
			<content:encoded><![CDATA[<pre><code><code>PHPUnit_Framework_Exception: Response from Selenium RC server for testComplete().
ERROR: Command execution failure. Please search the forum at http://clearspace.openqa.org for error details from the log window.
The error message is: Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsILocalFile.initWithPath]
</code></code></pre>
<p>Setting up Selenium RC server together with PHPUnit has proven surprisingly easy&#8230; Well most of it. Every now and then as a beginner you might run into monster errors like the one above. No fear, what Selenium is trying to tell you is just that your path is wrong.</p>
<p>For me it happened when trying to setup the  error screenshot variables:</p>
<pre><code><code>protected $captureScreenshotOnFailure = TRUE;
<strong>protected $screenshotPath = '/var/www/localhost/htdocs/screenshots';</strong>
protected $screenshotUrl = 'http://localhost/screenshots';
</code></code></pre>
<p>In case you wonder the path relates to the <strong>browser&#8217;s host</strong> not the Selenium RC host (if they are different), In my case the browsers are run on a windows box, with the Selenium RC setup on a Linux machine. Thus the error about the inapropriate <strong>$screenshotPath</strong>.</p>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2008/08/server-error-500-htaccess-require-valid-user/" rel="bookmark">Server Error 500 - htaccess require valid-user</a></li><li><a href="http://think-robot.com/2009/07/autloading-modular-forms-models-in-zend-framework-18/" rel="bookmark">Autloading modular forms & models in Zend Framework 1.8</a></li><li><a href="http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/" rel="bookmark">Zend_Db_Select multiple table joins explained</a></li><li><a href="http://think-robot.com/2008/10/how-to-convert-pdt-projects-into-phpeclipse-projects/" rel="bookmark">How to convert PDT projects into PHPEclipse projects</a></li><li><a href="http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/" rel="bookmark">Wordpress NextGen gallery tweak</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/12/phpunit-selenium-screenshot-path-problem/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Autloading modular forms &amp; models in Zend Framework 1.8</title>
		<link>http://think-robot.com/2009/07/autloading-modular-forms-models-in-zend-framework-18/</link>
		<comments>http://think-robot.com/2009/07/autloading-modular-forms-models-in-zend-framework-18/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 21:55:00 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[autload]]></category>

		<category><![CDATA[examples]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=200</guid>
		<description><![CDATA[It&#8217;s not really obvious with the error messages you get, but using a thing like Form_Login does not work out of the box with a modular structure. Which is slightly surprising considering that it&#8217;s inside the default module.
I have found examples like the second one below, but no one mentions that you actually declare the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not really obvious with the error messages you get, but using a thing like Form_Login does not work out of the box with a modular structure. Which is slightly surprising considering that it&#8217;s inside the default module.</p>
<p>I have found <a href="http://forums.zend.com/viewtopic.php?f=69&#038;t=1288&#038;start=0" class="external">examples like the second one below</a>, but no one mentions that you actually declare the default namespace too for everything to work.</p>
<pre><code><code>$autoloader = new Zend_Application_Module_Autoloader(array(
	'namespace' => '',
	'basePath'  => APPLICATION_PATH .'/modules/default',
	'resourceTypes' => array (
		'form' => array(
		'path' => 'forms',
		'namespace' => 'Form',
    ),
	'model' => array(
		'path' => 'models',
		'namespace' => 'Model',
    	),
    )
));

$autoloader = new Zend_Application_Module_Autoloader(array(
	'namespace' => 'Admin_',
	'basePath'  => APPLICATION_PATH .'/modules/admin',
	'resourceTypes' => array (
		'form' => array(
		'path' => 'forms',
		'namespace' => 'Form',
    ),
	'model' => array(
		'path' => 'models',
		'namespace' => 'Model',
    	),
    )
));</code></code></pre>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2009/02/zend-framework-decorators-labels-and-checkboxes/" rel="bookmark">Zend Framework Decorators - Labels and Checkboxes</a></li><li><a href="http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/" rel="bookmark">Doctrine Many To Many With Extra Fields</a></li><li><a href="http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/" rel="bookmark">Wordpress NextGen gallery tweak</a></li><li><a href="http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/" rel="bookmark">Zend_Db_Select multiple table joins explained</a></li><li><a href="http://think-robot.com/2009/12/phpunit-selenium-screenshot-path-problem/" rel="bookmark">PHPUnit & Selenium - screenshot path problem</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/07/autloading-modular-forms-models-in-zend-framework-18/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Doctrine Many To Many With Extra Fields</title>
		<link>http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/</link>
		<comments>http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/#comments</comments>
		<pubDate>Sat, 09 May 2009 10:25:13 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[database]]></category>

		<category><![CDATA[doctrine]]></category>

		<category><![CDATA[many to many]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[YAML]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=180</guid>
		<description><![CDATA[Recently I have started using Doctrine with Zend Framework. Most of the time it is great, but sometimes I get stuck on this or that issue. Most of my problems so far have been connected with the Many to Many relationship. Here are a few tips I learned the hard way.
Automatic relationship detection
When setting up [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have started using Doctrine with Zend Framework. Most of the time it is great, but sometimes I get stuck on this or that issue. Most of my problems so far have been connected with the Many to Many relationship. Here are a few tips I learned the hard way.<span id="more-180"></span></p>
<h2>Automatic relationship detection</h2>
<p>When setting up a YAML file with your database schema you can start it with the handy declaration:</p>
<pre><code><code>---
detect_relations: true</code></code></pre>
<p>It does the One to Many relationship for you nicely, however the Many to Many ones did not work out of the box and required manual BaseModel tweaking. Take for example my <strong>Tag</strong>, <strong>Website</strong>, and <strong>WebsiteTag</strong> classes.</p>
<h3>BaseTag:</h3>
<pre><code><code>abstract class BaseTag extends Doctrine_Record {

  public function setTableDefinition() {
    $this-&gt;setTableName('ts_tags');
    $this-&gt;hasColumn('name', 'string', 128, array('type' =&gt; 'string', 'length' =&gt; '128'));
    $this-&gt;hasColumn('is_category as isCategory', 'integer', 1, array('type' =&gt; 'integer', 'length' =&gt; '1'));
    $this-&gt;hasColumn('page_id', 'integer', 8, array('type' =&gt; 'integer', 'length' =&gt; 8));
    $this-&gt;hasColumn('ordering', 'integer', 5, array('type' =&gt; 'integer', 'length' =&gt; '5'));
  }

  public function setUp() {
    $this-&gt;hasOne('Page', array('local' =&gt; 'page_id',
                                              'foreign' =&gt; 'id'));
<strong>
    $this-&gt;hasMany('Website as Websites', array('local' =&gt; 'tag_id',
                                                                     'foreign' =&gt; 'website_id',
                                                                     'refClass' =&gt; 'WebsiteTag'));</strong>

    $i18n0 = new Doctrine_Template_I18n(array('fields' =&gt; array(0 =&gt; 'name')));
    $this-&gt;actAs($i18n0);
  }
}</code></code></pre>
<h3>BaseWebsite:</h3>
<pre><code><code>abstract class BaseWebsite extends Doctrine_Record{
  public function setTableDefinition() {
    $this-&gt;setTableName('ts_websites');
    $this-&gt;hasColumn('page_id', 'integer', 8, array('type' =&gt; 'integer', 'length' =&gt; 8));
    $this-&gt;hasColumn('title', 'string', 128, array('type' =&gt; 'string', 'length' =&gt; '128'));
    $this-&gt;hasColumn('client', 'string', 128, array('type' =&gt; 'string', 'length' =&gt; '128'));
    $this-&gt;hasColumn('link', 'string', 255, array('type' =&gt; 'string', 'length' =&gt; '255'));
    $this-&gt;hasColumn('intro', 'string', null, array('type' =&gt; 'string'));
    $this-&gt;hasColumn('content', 'string', null, array('type' =&gt; 'string'));
    $this-&gt;hasColumn('published', 'timestamp', null, array('type' =&gt; 'timestamp'));
    $this-&gt;hasColumn('is_home as isHome', 'integer', 1, array('type' =&gt; 'integer', 'length' =&gt; '1'));
  }

  public function setUp() {
    $this-&gt;hasOne('Page', array('local' =&gt; 'page_id',
                                              'foreign' =&gt; 'id'));

    <strong>$this-&gt;hasMany('Tag as Tags', array('local' =&gt; 'website_id',
                                                          'foreign' =&gt; 'tag_id',
                                                          'refClass' =&gt; 'WebsiteTag'));</strong>

    $this-&gt;hasMany('Media', array('local' =&gt; 'website_id',
                                                 'foreign' =&gt; 'media_id',
                                                 'refClass' =&gt; 'WebsiteMedia'));

    $i18n0 = new Doctrine_Template_I18n(array('fields' =&gt; array(0 =&gt; 'intro', 1 =&gt; 'content', 2 =&gt; 'title')));
    $this-&gt;actAs($i18n0);
  }
}
</code></code></pre>
<h3>BaseWebsiteTag:</h3>
<pre><code><code>abstract class BaseWebsiteTag extends Doctrine_Record {

  public function setTableDefinition() {
    $this-&gt;setTableName('ts_website_has_tags');
    $this-&gt;hasColumn('tag_id', 'integer', 8, array('type' =&gt; 'integer', 'length' =&gt; 8, <strong>'primary' =&gt; true )</strong>);
    $this-&gt;hasColumn('website_id', 'integer', 8, array('type' =&gt; 'integer', 'length' =&gt; 8,<strong> 'primary' =&gt; true</strong>));
  }

  public function setUp() {
  }
}
</code></code></pre>
<p>The highlighted portions of code needed to be added. You can actually define these manually in the YAML file, the catch is that I was expecting it to happen magically with the auto relationship setting.</p>
<h2>Many to Many with extra fields</h2>
<p>Another little issue that Doctrine has is when you try to retrieve records. It in a way skips the joining table and retrieves a collection of the final items. In most cases this is exactly what you might need. However for my Media I decided to keep the ordering in the joining table - WebsiteMedia.</p>
<pre><code><code>$q = Doctrine_Query::create()
  -&gt;from('Website w')
  -&gt;joinLeft('w.Media');</code></code></pre>
<p>If you simply proceed with the above you only get Media objects without the ordering.</p>
<h3>BaseWebsiteMedia:</h3>
<pre><code><code>abstract class BaseWebsiteMedia extends Doctrine_Record {

    public function setTableDefinition() {
        $this-&gt;setTableName('ts_website_has_media');
        $this-&gt;hasColumn('website_id', 'integer', 8, array('type' =&gt; 'integer', 'length' =&gt; 8, 'primary' =&gt; true));
        $this-&gt;hasColumn('media_id', 'integer', 8, array('type' =&gt; 'integer', 'length' =&gt; 8, 'primary' =&gt; true));
        $this-&gt;hasColumn('caption', 'string', 255, array('type' =&gt; 'string', 'length' =&gt; '255'));
        $this-&gt;hasColumn('is_main as isMain', 'integer', 1, array('type' =&gt; 'integer', 'length' =&gt; '1'));
        $this-&gt;hasColumn('ordering', 'integer', 4, array('type' =&gt; 'integer', 'length' =&gt; '4'));
    }

    public function setUp() {
        <strong>$this-&gt;hasOne('Website', array('local' =&gt; 'website_id',
                                       'foreign' =&gt; 'id'));

        $this-&gt;hasOne('Media', array('local' =&gt; 'media_id',
                                     'foreign' =&gt; 'id'));</strong>

        $i18n0 = new Doctrine_Template_I18n(array('fields' =&gt; array(0 =&gt; 'caption')));
        $this-&gt;actAs($i18n0);
    }
}</code></code></pre>
<p>If you do not have the additional One to Many relations declared already you should add them now. With the modified model now you can get the records like so:</p>
<pre><code><code>$q = Doctrine_Query::create()
  ->from('Website w')
  ->leftJoin('w.WebsiteMedia wm')
    ->leftJoin('wm.Media m');
</code></code></pre>
<p>This gives you a collection of WebsiteMedia records each containing the actual Media item.</p>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/" rel="bookmark">Zend_Db_Select multiple table joins explained</a></li><li><a href="http://think-robot.com/2009/02/zend-framework-decorators-labels-and-checkboxes/" rel="bookmark">Zend Framework Decorators - Labels and Checkboxes</a></li><li><a href="http://think-robot.com/2008/12/using-zend_mail-and-google-smtp-to-send-emails/" rel="bookmark">Using Zend_Mail and Google SMTP to send emails</a></li><li><a href="http://think-robot.com/2009/07/autloading-modular-forms-models-in-zend-framework-18/" rel="bookmark">Autloading modular forms & models in Zend Framework 1.8</a></li><li><a href="http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/" rel="bookmark">Wordpress NextGen gallery tweak</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Week of the Month in Mysql</title>
		<link>http://think-robot.com/2009/04/week-of-the-month-in-mysql/</link>
		<comments>http://think-robot.com/2009/04/week-of-the-month-in-mysql/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 15:37:16 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[Tutorials]]></category>

		<category><![CDATA[function]]></category>

		<category><![CDATA[Mysql]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[stackoverflow]]></category>

		<category><![CDATA[week]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=173</guid>
		<description><![CDATA[SELECT WEEK(my_date_field,5) -
WEEK(DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY),5)+1


Recently I needed to get the number of the week in a certain month. There is the handy WEEK() function, however it only gives you the week of the year.
Obviously before we can go through the code above, a definition of the nth week of the month is needed (as [...]]]></description>
			<content:encoded><![CDATA[<pre><code><code>SELECT WEEK(my_date_field,5) -
WEEK(DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY),5)+1
</code></code></pre>
<p><span id="more-173"></span><br />
Recently I needed to get the number of the week in a certain month. There is the handy <strong>WEEK()</strong> function, however it only gives you the week of the year.</p>
<p>Obviously before we can go through the code above, a definition of the <strong>n<sup>th</sup></strong> week of the month is needed (as I realized after <a href="http://stackoverflow.com/questions/785206/function-for-week-of-the-month-in-mysql">asking the question on stackoverflow</a>&#8230;):</p>
<p>For me the week starts on a Monday, and the first week of the month does not have to be a full week - if a month starts on a Friday, the Friday to Sunday days count as the first week.</p>
<p>With this out of the way here is a quick explanation of what goes on above.</p>
<pre><code><code>WEEK(my_date_field,<strong>5</strong>)</code></code></pre>
<p>First we get the actual week number of the year for the specified date. The second parameter is there to define Monday as the first day of the week (<a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_week">more details in Mysql manual</a>).</p>
<pre><code><code>DAYOFMONTH(my_date_field)</code></code></pre>
<p>Next we get the day of the month for the specified date.</p>
<pre><code><code>DATE_SUB(my_date_field, <strong>DAYOFMONTH(my_date_field)</strong>-1 DAY)</code></code></pre>
<p>We can use that number with <strong>DATE_SUB</strong> to get the date for the 1<sup>st</sup> day of the month. This in turn can be used to get the week number for the 1<sup>st</sup> week of the month.</p>
<pre><code><code>SELECT WEEK(my_date_field,5) -
WEEK(<strong>DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY)</strong>,5)+1
</code></code></pre>
<p>Finally we subtract the first day&#8217;s week from the actual week number which should give us the month&#8217;s week number starting from 0. Thus the final <strong>+1</strong> if you want the count to start from 1.</p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=55c87db5-a5a4-45e9-92f1-43c731c03ece" /><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2008/06/atmedia-london-2008/" rel="bookmark">@media - London, 2008</a></li><li><a href="http://think-robot.com/2009/02/firefox-ignores-tabs-but-not-spaces-in-a-pre-tag/" rel="bookmark">Firefox ignores tabs but not spaces in a pre tag</a></li><li><a href="http://think-robot.com/2009/02/how-to-use-the-strong-ownership-list/" rel="bookmark">How To Use the Strong Ownership List</a></li><li><a href="http://think-robot.com/2009/12/zend_date-time-part-and-gmt/" rel="bookmark">Zend_Date time part and GMT</a></li><li><a href="http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/" rel="bookmark">Zend_Db_Select multiple table joins explained</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/04/week-of-the-month-in-mysql/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Zend_Db_Select multiple table joins explained</title>
		<link>http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/</link>
		<comments>http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 22:52:23 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[Tutorials]]></category>

		<category><![CDATA[ORM]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[SQL]]></category>

		<category><![CDATA[Zend]]></category>

		<category><![CDATA[Zend Framework]]></category>

		<category><![CDATA[Zend_Db_select]]></category>

		<category><![CDATA[Zend_Db_Table_Abstract]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=156</guid>
		<description><![CDATA[It sounds like a simple task - retrieve the result from a join SQL query. Unusually you can even find documentation on the official Zend Framework site explaining how to put together a query that will return the results from a JOIN query. Unfortunately when it actually comes to putting theory into practice any Zend [...]]]></description>
			<content:encoded><![CDATA[<p>It sounds like a simple task - retrieve the result from a join SQL query. Unusually you can even find <a href="http://framework.zend.com/manual/en/zend.db.select.html" target="_blank">documentation on the official Zend</a> Framework site explaining how to put together a query that will return the results from a JOIN query. Unfortunately when it actually comes to putting theory into practice any Zend newcomer can run into several problems.<span id="more-156"></span></p>
<h2>You Think It&#8217;s Easy&#8230;</h2>
<p>For starters rather than use &#8220;raw&#8221; <strong>Zend_Db_Select</strong>:</p>
<pre><code><code>$select = $db-&gt;select();</code></code></pre>
<p>I wanted to derive the select from my <strong>Zend_Db_Table_Abstract</strong>, like so:</p>
<pre><code><code>
class Model_Db_Website extends Zend_Db_Table_Abstract {
	(...)
	public function fetchWebsites(){
		$select = $this-&gt;select();
		$select-&gt;join(array('whi' =&gt; 'website_has_images), 'whi.website_id = websites.website_id')
		return $this-&gt;fetchAll($select);
	}
}
</code></code></pre>
<p>This was more or less the example I found in the documentation. You might be wondering what is wrong with this picture. At first it seems like there is nothing missing. The table fields and the table name (for the FROM clause) are taken from the table class, and we have provided all the necessary JOIN details&#8230; However instead of the expected result, you an error message!</p>
<pre class="msg"><code><strong>Message:</strong> Select query cannot join with another table </code></code></pre>
<h2>The Solution</h2>
<p>Not to prolong any more here is the final bit, which we will walk-through below.</p>
<pre><code><code>
class Model_Db_Website extends Zend_Db_Table_Abstract {
	(...)
	public function fetchWebsites(){
		$select = $this-&gt;select();
		$select<strong>-&gt;setIntegrityCheck(false)</strong>
			<strong>-&gt;from($this-&gt;_name, '*')</strong>
			-&gt;join(
				array('whi' =&gt; 'website_has_images'),
				'whi.website_id = websites.website_id'<strong>, '*'</strong>
    			);
		return $this-&gt;fetchAll($select);
	}
}
</code></code></pre>
<p>The first thing you need to do is get rid of the error. This one has been mentioned online quite a lot and is easy to fix by applying:</p>
<pre class="msg"><code>-&gt;setIntegrityCheck(false)</code></code></pre>
<p>The code above makes your result set read only, but allows you to do joins.</p>
<p>Now for the less obvious bits. Once the above problem is fixed you will notice that your results only include the join table fields. To fix this add the final parameter to the list - either a &#8216;*&#8217; or an explicit list of fields.</p>
<p><strong>Many sites suggest using an empty array, though this causes the result set to return only main table fields&#8230;</strong></p>
<p>The &#8216;*&#8217; is not enough though. You still need the from declaration <strong>-&gt;from($this-&gt;_name, &#8216;*&#8217;)</strong>, as without you will still be getting just the join table fields.</p>
<p>So there you go, it took me quite a bit of fiddling and randomly changing the parameter set to arrive at the final solution. Hope it saves you the hassle too!</p>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/" rel="bookmark">Doctrine Many To Many With Extra Fields</a></li><li><a href="http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/" rel="bookmark">Wordpress NextGen gallery tweak</a></li><li><a href="http://think-robot.com/2009/04/week-of-the-month-in-mysql/" rel="bookmark">Week of the Month in Mysql</a></li><li><a href="http://think-robot.com/2009/02/zend-framework-decorators-labels-and-checkboxes/" rel="bookmark">Zend Framework Decorators - Labels and Checkboxes</a></li><li><a href="http://think-robot.com/2008/10/display-meetings-from-entourage-using-geektool/" rel="bookmark">Display Meetings From Entourage Using GeekTool</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Zend Framework Decorators - Labels and Checkboxes</title>
		<link>http://think-robot.com/2009/02/zend-framework-decorators-labels-and-checkboxes/</link>
		<comments>http://think-robot.com/2009/02/zend-framework-decorators-labels-and-checkboxes/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 11:16:12 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[forms]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[Zend]]></category>

		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.think-robot.com/?p=20</guid>
		<description><![CDATA[Very often it is the smallest things that annoy us the most. It took me a while to figure out why the placement parameter was not changing anything. I started of with the following:
'decorators' =&#62; array(
	array('Label'),
	array('ViewHelper')
)

Unfortunately switching the order between Label and ViewHelper did not change the order of elements. So I found this parameter [...]]]></description>
			<content:encoded><![CDATA[<p>Very often it is the smallest things that annoy us the most. It took me a while to figure out why the placement parameter was not changing anything. I started of with the following:</p>
<pre><code>'decorators' =&gt; array(
	array('Label'),
	array('ViewHelper')
)</code></pre>
<p><span id="more-20"></span><br />
Unfortunately switching the order between Label and ViewHelper did not change the order of elements. So I found this parameter called &#8220;placement&#8221;.</p>
<pre><code>'decorators' =&gt; array(
	array('Label', array('placement' =&gt; 'APPEND')),
	array('ViewHelper')
)</code></pre>
<p>It can take two values: PREPEND or APPEND. At this point however I did not realize that now the order of Label and ViewHelper did matter. Only the order below will make any difference to the html elements order.</p>
<pre><code>'decorators' =&gt; array(
	array('ViewHelper'),
	array('Label', array('placement' =&gt; 'APPEND'))
)</code></pre>
<p>This finally resulted in the desired effect.</p>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2009/07/autloading-modular-forms-models-in-zend-framework-18/" rel="bookmark">Autloading modular forms & models in Zend Framework 1.8</a></li><li><a href="http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/" rel="bookmark">Wordpress NextGen gallery tweak</a></li><li><a href="http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/" rel="bookmark">Zend_Db_Select multiple table joins explained</a></li><li><a href="http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/" rel="bookmark">Doctrine Many To Many With Extra Fields</a></li><li><a href="http://think-robot.com/2008/12/using-zend_mail-and-google-smtp-to-send-emails/" rel="bookmark">Using Zend_Mail and Google SMTP to send emails</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/02/zend-framework-decorators-labels-and-checkboxes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using Zend_Mail and Google SMTP to send emails</title>
		<link>http://think-robot.com/2008/12/using-zend_mail-and-google-smtp-to-send-emails/</link>
		<comments>http://think-robot.com/2008/12/using-zend_mail-and-google-smtp-to-send-emails/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 19:15:11 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[emails]]></category>

		<category><![CDATA[Google]]></category>

		<category><![CDATA[smtp]]></category>

		<category><![CDATA[Zend]]></category>

		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.think-robot.com/?p=21</guid>
		<description><![CDATA[You can set this globally, for example in your bootstrap.php file:
$tr = new Zend_Mail_Transport_Smtp('smtp.gmail.com', array(
	'auth' =&#62; 'login',
	'username' =&#62; 'YOUR_USERNAME@gmail.com',
	'password' =&#62; 'YOUR_PASSWORD',
	'ssl' =&#62; 'ssl',
	'port' =&#62; 465)
);
Zend_Mail::setDefaultTransport($tr);

And later just use the normal Zend_Mail function as usual:
$mail = new Zend_Mail();
$mail-&#62;setBodyHtml('Your html email here.');
$mail-&#62;addTo('send_to@mail.com');
$mail-&#62;setSubject('Mail Subject');
$mail-&#62;setFrom('from@mail.com');
$mail-&#62;send();
To quickly explain the settings:
You need to set the authentication type to login, and provide [...]]]></description>
			<content:encoded><![CDATA[<p>You can set this globally, for example in your bootstrap.php file:</p>
<pre><code>$tr = new Zend_Mail_Transport_Smtp('smtp.gmail.com', array(
	'auth' =&gt; 'login',
	'username' =&gt; 'YOUR_USERNAME@gmail.com',
	'password' =&gt; 'YOUR_PASSWORD',
	'ssl' =&gt; 'ssl',
	'port' =&gt; 465)
);
Zend_Mail::setDefaultTransport($tr);</code></pre>
<p><span id="more-21"></span></p>
<p>And later just use the normal Zend_Mail function as usual:</p>
<pre><code>$mail = new Zend_Mail();
$mail-&gt;setBodyHtml('Your html email here.');
$mail-&gt;addTo('send_to@mail.com');
$mail-&gt;setSubject('Mail Subject');
$mail-&gt;setFrom('from@mail.com');
$mail-&gt;send();</code></pre>
<p>To quickly explain the settings:<br />
You need to set the authentication type to login, and provide your Google email and password, as obviously to prevent spam Google needs some verification of who you are. Then we specify the ssl type: Zend supports tls and ssl, and for Google we need to specify the later one. This also means that we need to use port <strong>465</strong> instead of the standard <strong>25</strong>.</p>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2008/08/server-error-500-htaccess-require-valid-user/" rel="bookmark">Server Error 500 - htaccess require valid-user</a></li><li><a href="http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/" rel="bookmark">Doctrine Many To Many With Extra Fields</a></li><li><a href="http://think-robot.com/2008/10/display-meetings-from-entourage-using-geektool/" rel="bookmark">Display Meetings From Entourage Using GeekTool</a></li><li><a href="http://think-robot.com/2009/07/redirect-in-controller-plugin-zend-framework-18/" rel="bookmark">Redirect in controller plugin - Zend Framework 1.8</a></li><li><a href="http://think-robot.com/2008/08/opensuse-11-on-lenovo-thinkpad-x61-tablet-pc/" rel="bookmark">openSUSE 11 on Lenovo Thinkpad X61 (tablet pc)</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2008/12/using-zend_mail-and-google-smtp-to-send-emails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Wordpress NextGen gallery tweak</title>
		<link>http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/</link>
		<comments>http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 20:39:03 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
		
		<category><![CDATA[Code]]></category>

		<category><![CDATA[fix]]></category>

		<category><![CDATA[gallery]]></category>

		<category><![CDATA[NextGen]]></category>

		<category><![CDATA[uploads]]></category>

		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.think-robot.com/?p=18</guid>
		<description><![CDATA[Recently I found myself in that very anoying situation where a very good piece of software seemed to lack just this one function&#8230; again. I have been doing a gallery website and used the NextGen gallery plugin to display the photos. Unfortunatelly it was a case of a gallery per post with about 1 or [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I found myself in that very anoying situation where a very good piece of software seemed to lack just this one function&#8230; again. I have been doing a gallery website and used the <a href="http://wordpress.org/extend/plugins/nextgen-gallery/" target="_blank">NextGen gallery plugin</a> to display the photos. Unfortunatelly it was a case of a gallery per post with about 1 or 2 photos per gallery.</p>
<p>The biggest issue with this setting was the process of creating galleries. By default in NextGen you create the gallery on one page, upload the images in another, and then create new post yet elsewhere. But there had to be a way to at least merge the gallery creation with photo upload, especially considering a new gallery form consisted of a single &#8220;gallery name&#8221; field.<span id="more-18"></span>Surprisingly enough after checking the code, both actions are actually processed in the same place. Thus by adding a new field on the photo upload form this time consuming problem could be solved:</p>
<p>Edit the file: <strong>wp-content/plugins/nggallery/admin/wp25/addgallery.php (line: 15)</strong></p>
<pre><code>// link for the flash file
$swf_upload_link = NGGALLERY_URLPATH . 'admin/wp25/upload.php';
$swf_upload_link = wp_nonce_url($swf_upload_link, 'ngg_swfupload');
//flash doesn't seem to like encoded ampersands, so convert them back here
$swf_upload_link = str_replace('&amp;', '&amp;', $swf_upload_link);

$defaultpath = $ngg_options['gallerypath'];

if ($_POST['addgallery']<strong> || !empty($_POST['newgalleryname'])</strong>){
  <strong>if(!empty($_POST['newgalleryname']))
    $_POST['galleryname'] = $_POST['newgalleryname'];</strong>
  check_admin_referer('ngg_addgallery');
  $newgallery = attribute_escape($_POST['galleryname']);
  if (!empty($newgallery))
    <strong>$_POST['galleryselect'] = </strong>nggAdmin::create_gallery($newgallery, $defaultpath);
}
if ($_POST['zipupload']){</code></pre>
<p>Edit the file: <strong>wp-content/plugins/nggallery/admin/wp25/addgallery.php (line: 263)</strong></p>
<pre><code>	&lt;tr valign="top"&gt;
		&lt;th scope="row"&gt;&lt;?php _e('in to', 'nggallery') ;?&gt;&lt;/th&gt;
		&lt;td&gt;&lt;select name="galleryselect" id="galleryselect"&gt;
		&lt;option value="0" &gt;&lt;?php _e('Choose gallery', 'nggallery') ?&gt;&lt;/option&gt;
		&lt;?php
			$gallerylist = $wpdb-&gt;get_results("SELECT * FROM $wpdb-&gt;nggallery ORDER BY gid ASC");
			if(is_array($gallerylist)) {
				foreach($gallerylist as $gallery) {
					echo '&lt;option value="'.$gallery-&gt;gid.'" &gt;'.$gallery-&gt;name.' | '.$gallery-&gt;title.'&lt;/option&gt;'."n";
				}
			}
		?&gt;
		&lt;/select&gt;<strong> &lt;label&gt;or &lt;input type="text" name="newgalleryname"  /&gt;&lt;/label&gt;</strong>
		&lt;br /&gt;&lt;?php echo _e('Note : The upload limit on your server is ','nggallery') . "&lt;strong&gt;" . ini_get('upload_max_filesize') . "Byte&lt;/strong&gt;n"; ?&gt;
		&lt;br /&gt;&lt;?php if ((IS_WPMU) &amp;&amp; wpmu_enable_function('wpmuQuotaCheck')) display_space_usage(); ?&gt;&lt;/td&gt;
	&lt;/tr&gt;</code></pre>
<p>Edit the file: <strong>wp-content/plugins/nggallery/admin/wp25/functions.php (line: 77)</strong></p>
<pre><code>if ($result) {
	nggallery::show_error(__('Gallery', 'nggallery').' &lt;strong&gt;'.$galleryname.'&lt;/strong&gt; '.__('already exists', 'nggallery'));
	return false;
} else {
	$result = $wpdb-&gt;query("INSERT INTO $wpdb-&gt;nggallery (name, path, title) VALUES ('$galleryname', '$nggpath', '$gallerytitle') ");
	if ($result) nggallery::show_message(__('Gallery', 'nggallery').' &lt;strong&gt;'.$wpdb-&gt;insert_id." : ".$galleryname.'&lt;/strong&gt; '.__('successfully created!','nggallery')."
".__('You can show this gallery with the tag','nggallery').'&lt;strong&gt; &lt;/strong&gt;');
	<strong>return $wpdb-&gt;insert_id;</strong>
}  </code></pre>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2008/11/nested-sortable-using-jtree-clickable-links/" rel="bookmark">Nested sortable using jTree - clickable links</a></li><li><a href="http://think-robot.com/2009/04/zend_db_select-multiple-table-joins-explained/" rel="bookmark">Zend_Db_Select multiple table joins explained</a></li><li><a href="http://think-robot.com/2008/08/server-error-500-htaccess-require-valid-user/" rel="bookmark">Server Error 500 - htaccess require valid-user</a></li><li><a href="http://think-robot.com/2008/10/display-meetings-from-entourage-using-geektool/" rel="bookmark">Display Meetings From Entourage Using GeekTool</a></li><li><a href="http://think-robot.com/2009/05/doctrine-many-to-many-with-extra-fields/" rel="bookmark">Doctrine Many To Many With Extra Fields</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2008/11/wordpress-nextgen-gallery-tweak/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
