<?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; framework</title>
	<atom:link href="http://www.poetryofprogramming.com/tag/framework/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>Install Symfony &#8211; Windows XP / Vista + WAMP</title>
		<link>http://www.poetryofprogramming.com/symfony/install-symfony-framework-xp-vista-wamp/</link>
		<comments>http://www.poetryofprogramming.com/symfony/install-symfony-framework-xp-vista-wamp/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 19:02:11 +0000</pubDate>
		<dc:creator>Krzysztof Karolczak</dc:creator>
				<category><![CDATA[Installation]]></category>
		<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>

		<guid isPermaLink="false">http://www.poetryofprogramming.com/uncategorized/instalacja-symfonii-windows-xp-wamp/</guid>
		<description><![CDATA[How to quickly and easily install Symfony Framework on Windows? A 15 minute guide that will let you start the fun right away!
I assume that you&#8217;re working on Windows XP or Vista and starting from scratch &#8211; that means without any WWW server configured on the machine &#8211; this guide will cover all of that [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">How to quickly and easily install Symfony Framework on Windows? A 15 minute guide that will let you start the fun right away!</p>
<p style="text-align: justify;">I assume that you&#8217;re working on Windows XP or Vista and starting from scratch &#8211; that means without any WWW server configured on the machine &#8211; this guide will cover all of that <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .  Enjoy! <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <span id="more-5"></span></p>
<p align="justify"><strong>What will we need?</strong></p>
<ul>
<li> <a title="WAMP" href="http://www.wampserver.com/" target="_blank">WAMP</a> (version 2.0 at the moment of writing)</li>
<li>and&#8230; that&#8217;s all! <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
</ul>
<p align="justify">Using WAMP we&#8217;ll install PEAR and Symfony right afterwards, but starting from the beginning:</p>
<p align="justify"><strong>Part 1 &#8211; Installing WAMP</strong></p>
<p align="justify"><a href="http://www.wampserver.com/" target="_blank">WAMP</a> is a self-installing, all-in-the-box package with Apache, MySQL and PHP 5 (5.2.8+) &#8211; and that&#8217;s exactly what we need (there is also PHPMyAdmin and SQLitemanager included which is nice and may come in handy). Of course it&#8217;s not the only pack like that available on the internet,  very similar to WAMP is for example <a title="XAMPP" href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a> &#8211; the choice is up to you. You could also install all the components manually&#8230; but what&#8217;s the point? <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p align="justify">WAMP will auto-extract it&#8217;s files (let&#8217;s say under C:\wamp) and start off without any problems. <em>Voila!</em> &#8211; a running server with default configuration! That was fast, wasn&#8217;t it</p>
<p align="justify">You can configure WAMP&#8217;s elements by clicking its tray icon. (You can also translate WAMP to a variety of languages &#8211; right click on the icon and see for yourself <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p class="note" align="justify">If your WAMP fails to start and you get a warning that port 80 is being used (and you don&#8217;t have another HTTP server running on the machine) &#8211; it can be Skype. Make sure that in Skype&#8217;s options (Advanced &gt;&gt; Connection) the checkbox about using port 80 as alternative port for incoming connections is not ticked.</p>
<p align="justify">Symfony needs PHP-XSL and Apache URL Rewrite Module to function normally (which are off by default on most installs). Let&#8217;s start by activating the Rewrite module &#8211; left click on WAMP&#8217;s tray icon and than in <em>Apache &gt;&gt; Apache Modules </em>menu select <em>rewrite_module</em> (server will automatically reboot). Now it&#8217;s time for XSL, as in the step before: &#8211; in <em>PHP &gt;&gt;</em> <em> PHP Extension</em> menu look for <em>php_xsl</em> and click it. But there is one more <em>php.ini</em> file, which WAMP sometimes won&#8217;t change (no clue why) &#8211; we need to do it by hand, let&#8217;s open: <em>C:\wamp\bin\php\php5.2.8\php.ini</em> and remove &#8220;;&#8221; from the line:</p>
<blockquote><p align="justify">;extension=php_xsl.dll</p>
</blockquote>
<p align="justify">Now we should add environment variables to have access to PHP i MySQL under the command line. Right-click on <em>My Computer</em>, than <em>Properties</em>. Switch to <em>Advanced </em>tab and click the <em>Environment Variables</em> button. At the end of variable <strong>PATH</strong> let&#8217;s add something like <em><strong>;C:\wamp\bin\php\php5.2.8;C:\wamp\bin\mysql\mysql5.0.45\bin</strong></em> (paths to MySQL and PHP files separated by a semicolon).</p>
<p align="justify"><strong>Part 2 &#8211; PEAR Install</strong></p>
<p align="justify"><a title="PEAR" href="http://pear.php.net" target="_blank"><strong>PEAR</strong></a> (<em><strong>P</strong>HP <strong>E</strong>xtension and <strong>A</strong>pplication <strong>R</strong>epository</em>) is a PHP extension distribution system. Installation files are bundled with PHP by default, so this won&#8217;t take us long.</p>
<p align="justify">In the WAMP&#8217;s PHP directory (ie. C:\wamp\bin\php\php5.2.8\) run the <strong>go-pear.bat</strong> file. Follow the installation steps and answer the questions, the default config should be just fine.</p>
<p>Inside the PHP directory the installer created a <strong>PEAR_ENV.reg</strong> file, which after double-clicking will add all the PEAR variables to the registry &#8211; no need to do it by hand.</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>Part 3 &#8211; Finally the Symfony </strong></p>
<p>Open the command line and write:</p>
<blockquote><p>&gt; pear channel-discover pear.symfony-project.com</p>
</blockquote>
<p>If everything goes wel, you&#8217;ll see:</p>
<p style="padding-left: 30px;"><em>Adding Channel &#8220;pear.symfony-project.com&#8221; succeeded<br />
 Discovery of channel &#8220;pear.symfony-project.com&#8221; succeeded</em></p>
<p>So the time has come, write the magic line:</p>
<blockquote><p>&gt; pear install symfony/symfony</p>
</blockquote>
<p>The download of the package shouldn&#8217;t take long (~2 MB), and PEAR automates the whole process.</p>
<p>Finally, to check if Symfony is installed correctly write:</p>
<p style="padding-left: 30px;"><em>symfony -V</em></p>
<p>Which should print out on the screen the version of Symfony you&#8217;ve just downloaded.</p>
<p><strong>Part 4 &#8211; Your first Symfony project</strong></p>
<p>Considering everything went smooth you can now write in the command line:</p>
<blockquote><p>&gt; cd C:\wamp\www<br />
 &gt; mkdir myproject<br />
 &gt; cd myproject<br />
 &gt; symfony init-project myproject<br />
 &gt; symfony init-app testapp</p>
</blockquote>
<p>For Symfony 1.2 the proper way to init a project is:</p>
<blockquote><p>&gt; symfony generate:project myproject<br />
 &gt; symfony generate:app &#8212;escaping-strategy=on &#8212;csrf-secret=Unique$ecret testapp</p>
</blockquote>
<p style="text-align: justify;">When calling the generate:app task, we have also passed two security related options:</p>
<ul>
<li><em>&#8211;escaping-strategy</em> &#8211; Enables output escaping to prevent <a href="http://en.wikipedia.org/wiki/Cross-site_scripting" target="_blank">XSS attacks</a></li>
<li><em>&#8211;csrf-secret</em> &#8211; Enables session tokens in forms to prevent <a href="http://en.wikipedia.org/wiki/CSRF" target="_blank">CSRF attacks</a></li>
</ul>
<p style="text-align: justify;">Yes, Symfony 1.2 will automatically protect us from the two most widespread vulnerabilities found on the web!</p>
<p><strong>Congratulations!</strong></p>
<p>The effects of your work can be seen at <em>http://127.0.0.1/</em>myproject/<em>web/</em></p>
<p><strong>And that&#8217;s that. I wish you many successful projects in Symfony <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </strong></p>
<p><strong>If you want to move on from here (with your brand new Symfony Framework on-board <img src='http://www.poetryofprogramming.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ):</strong></p>
<ul style="text-align: justify;">
<li><a title="Setting the web server to gain access to symfony_data/web/sf/ directory" href="http://www.poetryofprogramming.com/symfony/setting-the-web-server-to-gain-access-to-symfony_datawebsf-directory/" target="_blank">See my guide on configuring Apache to work hand in hand with your Symfony applications.</a></li>
<li><a title="Symfony Framework" href="http://www.symfony-project.org/" target="_blank">Visit the Symfony official web page and enjoy the variety of guides, tutorials and well written documentation. </a></li>
</ul>
<p 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/symfony/install-symfony-framework-xp-vista-wamp/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
		</item>
	</channel>
</rss>
