<?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>Poetry of Programming &#187; tips</title>
	<atom:link href="http://www.poetryofprogramming.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.poetryofprogramming.com</link>
	<description>Because coding is art</description>
	<lastBuildDate>Wed, 15 Apr 2009 15:05:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>10 short answers to frequently asked questions about Symfony</title>
		<link>http://www.poetryofprogramming.com/symfony/10-short-answers-to-frequently-asked-questions-about-symfony/</link>
		<comments>http://www.poetryofprogramming.com/symfony/10-short-answers-to-frequently-asked-questions-about-symfony/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 14:57:21 +0000</pubDate>
		<dc:creator>Krzysztof Karolczak</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Symfony 1.0]]></category>
		<category><![CDATA[Symfony 1.1]]></category>
		<category><![CDATA[Symfony 1.2]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.poetryofprogramming.com/?p=39</guid>
		<description><![CDATA[I wanted to put together few simple answers to problems which I came across when browsing through Symfony related forums and mailing lists. The list is of course open and please feel free to write a comment with your own question. I&#8217;ll be glad to help if I only can  .
Remark: The answers generally [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to put together few simple answers to problems which I came across when browsing through Symfony related forums and mailing lists. The list is of course open and please feel free to write a comment with your own question. I&#8217;ll be glad to help if I only can <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<span id="more-39"></span></p>
<p style="text-align: justify;"><strong>Remark:</strong> The answers generally apply to all the versions of Symfony if not specified otherwise. And the exemplary code uses Propel.</p>
<h4 style="text-align: justify;">1. How to gain access to the Request object outside the action?</h4>
<p style="text-align: justify;">In any place in your Symfony project you can initialize an instance of sfContext by simply calling <em>sfContext::getInstance() </em>and use it to retrieve other important object including the Request.</p>
<p style="text-align: justify;">For example:</p>
<p style="text-align: justify; padding-left: 30px;">$context = sfContext::getInstance();<br />
 $context-&gt;getActionName();<br />
 $context-&gt;getActionStack();<br />
 $context-&gt;getController();<br />
 $context-&gt;getDatabaseConnection();<br />
 $context-&gt;getLogger();<br />
 $context-&gt;getModuleDirectory();<br />
 $context-&gt;getModuleName();<br />
 $context-&gt;getRequest();<br />
 $context-&gt;getResponse();<br />
 $context-&gt;getUser();</p>
<h4 style="text-align: justify;">2. How to make  an <em>object_select_tag</em> create a drop down containing a sorted list?</h4>
<p style="text-align: justify;">Let&#8217;s say that we have an object <em>$record</em> with <em>person_id</em> field, which is a foreign reference to a Person class. We want in our form to display <em>object_select_tag</em> that will contain all the possible choices of <em>Person</em> objects sorted for example by their <em>name</em>.</p>
<p style="text-align: justify;">By calling:</p>
<p style="text-align: justify; padding-left: 30px;">object_select_tag ($record, &#8216;getPersonId&#8217;);</p>
<p style="text-align: justify;">We only get a list sorted by id (probably)  &#8211; because <em>object_select_tag</em> uses <em>PersonPeer::doSelect()</em> by default.</p>
<p style="text-align: justify;">So the solution is:</p>
<p style="text-align: justify; padding-left: 30px;">object_select_tag ($record, &#8216;getPersonId&#8217;, array (</p>
<p style="text-align: justify; padding-left: 60px;">&#8216;related_class&#8217; =&gt; &#8216;Person&#8217;,<br />
 <strong> &#8216;peer_method&#8217; =&gt; &#8216;doOrderedSelect&#8217;,</strong><br />
 &#8216;include_blank&#8217; =&gt; true)</p>
<p style="text-align: justify; padding-left: 30px;">) ;</p>
<p style="text-align: justify;">And we just need to create a <em>doOrderedSelect</em> method in <em>PersonPeer </em>class, that might look like this:</p>
<p style="padding-left: 30px;">public static function doOrderedSelect($c = &#8221;)<br />
 {</p>
<p style="padding-left: 60px;">If(!$c)<br />
 $c = new Criteria();</p>
<p style="padding-left: 60px;">$c-&gt;addAscendingOrderByColumn(PersonPeer::NAME);<br />
 return parent::doSelect($c);</p>
<p style="padding-left: 30px;">}</p>
<p style="text-align: justify;">And remember that for all of that to work <em>Person</em> class has to have a __toString() method [see point 3].</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-5856107143683355";
google_ad_slot = "0824560404";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<h4 style="text-align: justify;">3. The magic <em>__toString() </em>method.</h4>
<p style="text-align: justify;">The most commonly used PHP5 magic method  in Symfony projects is the <em>__toString()</em> method, which specifies what value should be returned when the given object is cast to string (or simply used as a string).</p>
<p style="text-align: justify;">It is a very natural programming approach to specify what &#8216;natural language phrase&#8217; should be associated with an object instance.  A short example to illustrate that:</p>
<p style="text-align: justify; padding-left: 30px;">class Person extends BasePerson<br />
 {</p>
<p style="text-align: justify; padding-left: 60px;">public function __toString()<br />
 {</p>
<p style="text-align: justify; padding-left: 90px;">return $this-&gt;getName() . &#8216; &#8216; . $this-&gt;getSurname();</p>
<p style="text-align: justify; padding-left: 60px;">}</p>
<p style="text-align: justify; padding-left: 30px;">}</p>
<p style="text-align: justify;">And now we can print out the name and surname of our <em>Person</em> class instance &#8211; let&#8217;s say <em>$example_person &#8211; </em>simply by writing <em>echo </em><em>$example_person</em>.</p>
<p style="text-align: justify;">The <em>__toString()</em> method is used by some of the Symfony form helpers and it&#8217;s usually worth spending a bit extra time to create those methods in our model &#8211; just for the sake of code readabilty.</p>
<p style="text-align: justify;">But be careful when using PHP version older then 5.2 -because  <em>__toString()</em> method was only called when it was directly combined with echo() or print(), which might cause unexpected results sometimes.</p>
<h4>4. How to create a paginated list in Symfony?</h4>
<p>Symfony comes with a <em>sfPropelPager </em>which handles all the paging for us. I hope the preceding code is self-explanatory.</p>
<p>The action:</p>
<p style="padding-left: 30px;">public function executeList()<br />
 {</p>
<p style="padding-left: 60px;">&#8230;</p>
<p style="padding-left: 60px;">$rows_per_page = sfConfig::get(&#8217;app_rows_per_page&#8217;); // Now you can set the number of rows per page in <em>your_app/config/app.yml</em></p>
<p style="padding-left: 60px;">$page = $this-&gt;getRequestParameter(&#8217;page&#8217;, 1);</p>
<p style="padding-left: 60px;">$pager = new sfPropelPager(&#8217;YourClass&#8217;, $rows_per_page);<br />
 $pager-&gt;setCriteria($c);<br />
 $pager-&gt;setPage($page);<br />
 $pager-&gt;init();<br />
 $this-&gt;pager = $pager;</p>
<p style="padding-left: 30px;">}</p>
<p>The template:</p>
<p style="padding-left: 30px;">&lt;?php if ($pager-&gt;haveToPaginate()): ?&gt;<br />
 &lt;div class=&#8221;pagination&#8221;&gt;</p>
<p style="padding-left: 30px;">&lt;?php echo link_to(&#8217;&lt;&lt; &#8216;, &#8216;example/list?page=&#8217;.$pager-&gt;getFirstPage()) ?&gt;<br />
 &lt;?php echo link_to(&#8217; &lt;&#8217;, &#8216;example/list?page=&#8217;.$pager-&gt;getPreviousPage()) ?&gt;</p>
<p style="padding-left: 30px;">&lt;?php $links = $pager-&gt;getLinks(); foreach ($links as $page): ?&gt;<br />
 &lt;?php echo ($page == $pager-&gt;getPage()) ? $page : link_to($page, &#8216;example/list?page=&#8217;.$page) ?&gt;<br />
 &lt;?php endforeach ?&gt;</p>
<p style="padding-left: 30px;">&lt;?php echo link_to(&#8217;&gt; &#8216;, &#8216;example/list?page=&#8217;.$pager-&gt;getNextPage()) ?&gt;<br />
 &lt;?php echo link_to(&#8217; &gt;&gt;&#8217;, &#8216;example/list?page=&#8217;.$pager-&gt;getLastPage()) ?&gt;</p>
<p style="padding-left: 30px;">&lt;/div&gt;<br />
 &lt;?php endif; ?&gt;</p>
<p style="padding-left: 30px; text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-5856107143683355";
google_ad_slot = "0824560404";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<h4>5. How to use pager with a custom peer method like <em>doSelectJoinAll()</em>?</h4>
<p>Let&#8217;s assume that we have a pager used to display a list of objects with foreign references and we want to sort it by one of the fields from a foreign table. We&#8217;ll need to use o <em>join</em> for that and it would be nice if we could use one of the generated methods like <em>doSelectJoin*()</em>, e.g. <em>doSelectJoinAll()</em>.</p>
<p>The solution is very simple. Before calling  <em>$pager-&gt;init()</em> use the pager&#8217;s <em>setPeerMethod().</em> In  our case that would be:</p>
<p style="padding-left: 30px;">$pager-&gt;setPeerMethod(&#8217;doSelectJoinAll&#8217;);</p>
<h4>6. Problem with storing dates before year 1970 in some databases.</h4>
<p>It was an issue for Symfony 1.0.X &#8211; don&#8217;t know if it applies to other versions as well.</p>
<p>For some db the data type <em>date</em> didn&#8217;t work as expected for dates prior to 01.01.1970. There exist a special, additional date type <em>bu_date</em> that you have to use in your <em>schema.yml</em>.</p>
<p style="padding-left: 30px;">date_of_birth:  {  type: bu_date  }</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-5856107143683355";
google_ad_slot = "0824560404";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<h4 style="text-align: justify;">7. JavaScript in Ajax response call is not executed.</h4>
<p>In Symfony if the response code of the Ajax call contains JavaScript it won&#8217;t be executed by default &#8211; for security reasons.</p>
<p>You need to explicitly declare for Ajax helpers the ability to execute scripts in remote responses &#8211; which can be done with the <em>script</em> option.</p>
<p>Using the example from Symfony&#8217;s documentation:</p>
<p style="padding-left: 30px;">&lt;?php<br />
 // If the response of the post/delete action contains JavaScript,<br />
 // allow it to be executed by the browser<br />
 echo link_to_remote(&#8217;Delete this post&#8217;, array(<br />
 &#8216;update&#8217; =&gt; &#8216;feedback&#8217;,<br />
 &#8216;url&#8217;    =&gt; &#8216;post/delete?id=&#8217;.$post-&gt;getId(),<br />
 &#8217;script&#8217; =&gt; true,<br />
 )) ?&gt;</p>
<p style="text-align: left;"><strong>8. 9. 10.</strong> &#8230; are in progress, and I&#8217;m open to <strong>your</strong> questions <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.poetryofprogramming.com/symfony/10-short-answers-to-frequently-asked-questions-about-symfony/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Maximum execution time exceeded error workaround</title>
		<link>http://www.poetryofprogramming.com/php/php-maximum-execution-time-exceeded-error-workaround/</link>
		<comments>http://www.poetryofprogramming.com/php/php-maximum-execution-time-exceeded-error-workaround/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 13:01:03 +0000</pubDate>
		<dc:creator>Krzysztof Karolczak</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.poetryofprogramming.com/uncategorized/php-maximum-execution-time-exceeded-error-workaround/</guid>
		<description><![CDATA[The Maximum execution time of 30 seconds exceeded error can be annoying when executing &#8220;heavy&#8221; PHP scripts (involving lots of data processing) and it&#8217;s also a frequent follow-up of running Apache web servers on Windows machines   (for example when testing Symfony projects on your local PC). But no worries!   &#8211; there [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">The <em>Maximum execution time of 30 seconds exceeded </em>error can be annoying when executing &#8220;heavy&#8221; PHP scripts (involving lots of data processing) and it&#8217;s also a frequent follow-up of running Apache web servers on Windows machines <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  (for example when testing Symfony projects on your local PC). But no worries! <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  &#8211; there are couple of easy ways to solve this problem. <span id="more-12"></span></p>
<p align="justify"><strong>Solution 1 &#8211; When we have access to PHP configurations files. </strong></p>
<p align="justify">If  we can edit the PHP config files the problem boils down to changing one variable in PHP.ini. Usually the right PHP.ini can be found in the main PHP dir (for example in my PHP WAMP intallation it&#8217;s located in <em>C:\wamp\bin\php\php5.2.5)</em>. And now we just need to open it and look for the line:</p>
<blockquote><p>max_execution_time = 30</p></blockquote>
<p align="justify">Changing the 30 seconds setting to something like 120 seconds should do the trick. And all we need to do now is to restart the web server. <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-5856107143683355";
google_ad_slot = "0824560404";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p><strong>Solution 2 &#8211; On a remote server.</strong></p>
<p align="justify">There is a nice PHP function called <span class="methodname"><em><strong><strong>set_time_limit ( </strong></strong>int $seconds<strong><strong> )</strong></strong></em>, every time it&#8217;s called it sets the timeout counter with the given value, which means that the script will now be able to run for <em>$seconds</em>. Execution of <em><strong>set_time_limit(0) </strong></em>removes the PHP timeout limit.</span></p>
<p align="justify">Unfortunately this will not work if PHP is running in <em>safe mode</em>. For further details consult the <a href="http://php.net/set_time_limit" target="_blank">PHP manual</a>.</p>
<p><strong>Other timeout possibilities <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </strong></p>
<p align="justify">It&#8217;s also worth mentioning that most of webservers have their own timeout counters &#8211; e.g. in Apache it&#8217;s set to 300 s by default. 6 minutes is usually plenty of time but it still can be changed if we desire so <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . The timeout setting can be found in <em>httpd-default.conf </em>file which is located with the rest of Apache conf files (e.g. my path is <em>C:\wamp\bin\apache\apache2.2.6\conf\extra</em>).</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-5856107143683355";
google_ad_slot = "0824560404";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.poetryofprogramming.com/php/php-maximum-execution-time-exceeded-error-workaround/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
