<?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>Janusz Slota &#187; Howto</title>
	<atom:link href="http://janusz.slota.name/blog/category/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://janusz.slota.name/blog</link>
	<description>My home page</description>
	<lastBuildDate>Thu, 08 Dec 2011 09:56:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>How to handle sfError404Exception</title>
		<link>http://janusz.slota.name/blog/2011/12/how-to-handle-sferror404exception/</link>
		<comments>http://janusz.slota.name/blog/2011/12/how-to-handle-sferror404exception/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 18:42:46 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[symfony1]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=227</guid>
		<description><![CDATA[The problem: Let&#8217;s say you need to migrate old website to you new Symfony app. You want to keep all old links working. When they do not match the new link scheme, you want to redirect with 301 (or 302) to correct page in the new URL scheme. All these can be managed via backend [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The problem:</strong></p>
<p>Let&#8217;s say you need to migrate old website to you new Symfony app. You want to keep all old links working. When they do not match the new link scheme, you want to redirect with 301 (or 302) to correct page in the new URL scheme. All these can be managed via backend module.</p>
<p>My first thought was I need a filter to catch sfError404Exception and handle redirections from there. Well, in Symfony 1.4 you can&#8217;t do it without changing the Symfony core files, so it&#8217;s not an option. This is because <strong>the filters are not even get called</strong>, as <strong>routing</strong> finds out first, that the module and/or action does not exists and throws this exception.</p>
<p><strong>The solution:</strong><br />
<span id="more-227"></span><br />
There is the easier way of doing this. First you need to add new route that catches all that does not match current link scheme. <strong>In fact you need to replace</strong> <em>default</em> and <em>default_index</em> routes with something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;"># apps/frontend/config/routing.yml
&nbsp;
your_other_routes_here: ~
&nbsp;
# generic rules
# please, remove them by adding more specific rules
# default_index:
#  url:   /:module
#  param: { action: index }
&nbsp;
# default:
#  url:   /:module/:action/*
&nbsp;
# at the very end of the file
catchall:
  url:   /*
  param: { module: default, action: catchall }</pre></div></div>

<p>If you still need <em>default</em> and <em>default_index</em> routes you need to somehow handle forwards in your executeCatchall action. Please note that the above <em>catchall</em> route cannot be combined together with <em>default</em> and <em>default_index</em>. If you put catchall above these two, they won&#8217;t be executed ever, on the other hand &#8211; if you put it below these two, <em>catchall</em> will never be executed.</p>
<p>Then in this case you need to create executeCatchall action in your default module:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// apps/frontend/modules/default/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> defaultActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeCatchall<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// find your redirectation using $request-&gt;getPathInfo()</span>
    <span style="color: #000088;">$redirection</span> <span style="color: #339933;">=</span> Doctrine_Core<span style="color: #339933;">::</span><span style="color: #004000;">getTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Redirection'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findOneByPathInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// this ensures that if there is no specified redirection, the old sfError404Exception gets thrown</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">forward404Unless</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$redirection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// redirect</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$redirection</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'redirect_to'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the above example I&#8217;m assuming the following model class:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">Redirection:
  columns:
    path_info:        { type: string(255) }
    redirect_to:      { type: string(255) }</pre></div></div>

<p>Of course this is very basic implementation of redirection functionality, but it shows the correct way of doing it. I hope it&#8217;ll help.</p>
<p><strong>What&#8217;s wrong with handling sfError404Exception inside the execute404() action?</strong></p>
<p>Well, it often happens that you need to throw sfError404Exception in your actions. It doesn&#8217;t feel right for me to throw this exception inside the method that handles it.</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2011/12/how-to-handle-sferror404exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to set default value or option in the Symfony embeded form inside the Action</title>
		<link>http://janusz.slota.name/blog/2011/09/how-to-set-default-value-or-option-in-the-symfony-embeded-form-inside-the-action/</link>
		<comments>http://janusz.slota.name/blog/2011/09/how-to-set-default-value-or-option-in-the-symfony-embeded-form-inside-the-action/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 13:23:56 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[embedded forms]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[symfony1]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=210</guid>
		<description><![CDATA[Let&#8217;s say you have Symfony 1.4 form User with embedded form Entry class UserForm extends Something &#123; public function configure&#40;&#41; &#123; $this-&#62;embedForm&#40;'entry', new EntryForm&#40;&#41;&#41;; &#125; &#125; and let&#8217;s say that this embedded form contains sfWidgetFormChoice widget class EntryForm extends BaseEntryForm &#123; public function configure&#40;&#41; &#123; $this-&#62;widgetSchema&#91;'cars'&#93; = new sfWidgetFormChoice&#40;array&#40; 'choices' =&#62; array&#40;&#41;, 'expanded' =&#62; true [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have Symfony 1.4 form User with embedded form Entry</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UserForm <span style="color: #000000; font-weight: bold;">extends</span> Something
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">embedForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'entry'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> EntryForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-210"></span><br />
and let&#8217;s say that this embedded form contains sfWidgetFormChoice widget</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> EntryForm <span style="color: #000000; font-weight: bold;">extends</span> BaseEntryForm
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormChoice<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'choices'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'expanded'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfValidatorChoice<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'choices'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Mind that choices option is an empty array &#8211; I want to set this option in the action as it depends on data in the session.</p>
<p>I found that, the following code in the action layer doesn&#8217;t work:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> defaultActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEmbeddedForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getWidget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'choices'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Not sure why, but it&#8217;d work if the cars widget is attached to the UserForm. Maybe it&#8217;s related to <a href="http://trac.symfony-project.org/ticket/6548">this bug</a>. Anyway, if try to set option choices, it sets correctly, but the view layer ignores it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> defaultActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEmbeddedForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getWidget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'choices'</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEmbeddedForm</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getWidget</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'choices'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setting up options for embedded forms works correctly, but the view layer seems to ignore it.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, if you have trouble setting up the dynamic options for the embedded form, you can do this, which seems to work.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> defaultActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getWidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'choices'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Same problem is with validatorSchema. I found that this seems to work:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> defaultActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UserForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getWidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'choices'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// validator part</span>
    <span style="color: #000088;">$validatorSchema</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValidatorSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$validatorSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'entry'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOption</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'choices'</span><span style="color: #339933;">,</span><span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cars'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I hope it&#8217;ll help.</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2011/09/how-to-set-default-value-or-option-in-the-symfony-embeded-form-inside-the-action/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>bash: /usr/bin/kvm: No such file or directory on Gentoo</title>
		<link>http://janusz.slota.name/blog/2011/05/bash-usrbinkvm-no-such-file-or-directory-on-gentoo/</link>
		<comments>http://janusz.slota.name/blog/2011/05/bash-usrbinkvm-no-such-file-or-directory-on-gentoo/#comments</comments>
		<pubDate>Tue, 31 May 2011 10:57:42 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[gentoo]]></category>
		<category><![CDATA[kvm]]></category>
		<category><![CDATA[qemu]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=192</guid>
		<description><![CDATA[After updating my Gentoo host machine yesterday with emerge -avDNu world, which included update of kvm from app-emulation/qemu-kvm-0.13.0-r2 to app-emulation/qemu-kvm-0.14.1, I wasn&#8217;t able to boot my guests systems any more. So first thing I did was emerge qemu-kvm. It turns out that it doesn&#8217;t help &#8211; kvm file is still missing. gentoo1 ~# ls -l [...]]]></description>
			<content:encoded><![CDATA[<p>After updating my Gentoo host machine yesterday with <strong>emerge -avDNu world</strong>, which included update of kvm from <strong>app-emulation/qemu-kvm-0.13.0-r2</strong>  to <strong>app-emulation/qemu-kvm-0.14.1</strong>, I wasn&#8217;t able to boot my guests systems any more. So first thing I did was <strong>emerge qemu-kvm</strong>. It turns out that it doesn&#8217;t help &#8211; kvm file is still missing. </p>
<pre>
gentoo1 ~# ls -l /usr/bin/ | grep kvm
-rwxr-xr-x 1 root root      66872 May 28 16:03 kvm.kss
</pre>
<p>Luckily for me I do have more Gentoo machines with KVM virtualization.</p>
<p>I&#8217;ve logged in on my other KVM Gentoo machine and found out following:</p>
<pre>
gentoo2 ~# ls -l /usr/bin/ | grep kvm
lrwxrwxrwx 1 root root         17 Nov 11  2010 kvm -> /usr/bin/qemu-kvm
-rwxr-xr-x 1 root root         61 Nov 11  2010 qemu-kvm
</pre>
<p>OK &#8211; so what&#8217;s the content of the qemu-kvm file?</p>
<pre>
gentoo2 ~# cat /usr/bin/qemu-kvm
#!/bin/sh
exec /usr/bin/qemu-system-x86_64 --enable-kvm "$@"
</pre>
<p>From the above you can easily fix the problem by creating qemu-kvm file with content above, plus adding the kvm symlink. Hope it&#8217;ll help.</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2011/05/bash-usrbinkvm-no-such-file-or-directory-on-gentoo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL FROM_UNIXTIME and UNIX_TIMESTAMP functions in PostgreSQL</title>
		<link>http://janusz.slota.name/blog/2009/06/mysql-from_unixtime-and-unix_timestamp-functions-in-postgresql/</link>
		<comments>http://janusz.slota.name/blog/2009/06/mysql-from_unixtime-and-unix_timestamp-functions-in-postgresql/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 20:00:42 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=80</guid>
		<description><![CDATA[Personally I prefer PostgreSQL over MySQL DBMS. However there are some things I miss in PostgreSQL. Here is an advice on how to use MySQL FROM_UNIXTIME and UNIX_TIMESTAMP function in PostgreSQL DBMS. FROM_UNIXTIME() MySQL query: mysql&#62; SELECT FROM_UNIXTIME&#40;123456789&#41;; PostgreSQL equivalent: postgres=&#62; SELECT to_timestamp&#40;123456789&#41;; -- with time zone postgres=&#62; SELECT to_timestamp&#40;123456789&#41;::TIMESTAMP; -- without time zone In [...]]]></description>
			<content:encoded><![CDATA[<p>Personally I prefer PostgreSQL over MySQL DBMS. However there are some things I miss in PostgreSQL. Here is an advice on how to use MySQL <strong>FROM_UNIXTIME</strong> and <strong>UNIX_TIMESTAMP</strong> function in PostgreSQL DBMS.<br />
<span id="more-80"></span></p>
<h2>FROM_UNIXTIME()</h2>
<p>MySQL query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> FROM_UNIXTIME<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>PostgreSQL equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> to_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">-- with time zone</span>
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> to_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>::<span style="color: #993333; font-weight: bold;">TIMESTAMP</span>; <span style="color: #808080; font-style: italic;">-- without time zone</span></pre></div></div>

<p>In order to be able to run MySQL style query in PostgreSQL DBMS, you need to create this function:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> from_unixtime<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">INTEGER</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	 SELECT to_timestamp($1)::timestamp AS result
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;</pre></div></div>

<p>then you can run query like this (exactly the same as in MySQL):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> FROM_UNIXTIME<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>;
    from_unixtime
<span style="color: #808080; font-style: italic;">---------------------</span>
 <span style="color: #cc66cc;">1973</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">29</span> <span style="color: #cc66cc;">21</span>:<span style="color: #cc66cc;">33</span>:09
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">ROW</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You can probably find (on the Internet) definitions with keyword <code>abstime</code> (i.e. <code>$1::abstime::timestamp without time zone AS result</code>).<br />
This keyword is obsolete as described <a href="http://www.postgresql.org/docs/current/interactive/datetime-keywords.html#DATETIME-MOD-TABLE">here</a>, so IMHO it&#8217;s better to use the definition you see above.</p>
<h2>UNIX_TIMESTAMP()</h2>
<p>MySQL queries:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>PostgreSQL equivalents:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">EXTRACT</span><span style="color: #66cc66;">&#40;</span>EPOCH <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">EXTRACT</span><span style="color: #66cc66;">&#40;</span>EPOCH <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #ff0000;">'1973-11-29 21:33:09'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Again &#8211; in order to be able to run MySQL style query in PostgreSQL DBMS, you need to create 3 functions:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- no params</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> unix_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	SELECT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))::bigint AS result;
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- timestamp without time zone (i.e. 1973-11-29 21:33:09)</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> unix_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TIMESTAMP</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	SELECT EXTRACT(EPOCH FROM $1)::bigint AS result;
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- timestamp with time zone (i.e. 1973-11-29 21:33:09+01)</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> unix_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">TIME</span> zone<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">BIGINT</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	SELECT EXTRACT(EPOCH FROM $1)::bigint AS result;
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;</pre></div></div>

<p>As noted <a href="http://www.postgresql.org/docs/8.0/static/datatype-datetime.html">here</a>, prior to PostgreSQL 7.3, writing just timestamp was equivalent to timestamp with time zone. This was changed for SQL compliance.</p>
<p>Now you can run queries like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09'</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09'</span> AT <span style="color: #993333; font-weight: bold;">TIME</span> ZONE <span style="color: #ff0000;">'GMT'</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09+01'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Tested on: PostgreSQL 8.1.11 (debian), PostgreSQL 8.2.7 (gentoo) and MySQL 5.0.70 (gentoo)</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2009/06/mysql-from_unixtime-and-unix_timestamp-functions-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What&#8217;s the average length of an email address?</title>
		<link>http://janusz.slota.name/blog/2009/05/email-length/</link>
		<comments>http://janusz.slota.name/blog/2009/05/email-length/#comments</comments>
		<pubDate>Tue, 05 May 2009 16:52:18 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=1</guid>
		<description><![CDATA[This question always comes up, when you have to design new table in database to store email addresses. Is VARCHAR(30) long enough? Or maybe you should use VARCHAR(255) or maybe TEXT as I&#8217;ve seen in some projects? My live database, which I&#8217;ve used for this test contains 92298 valid email addresses, verified with the Pear [...]]]></description>
			<content:encoded><![CDATA[<p>This question always comes up, when you have to design new table in database to store email addresses. Is <strong>VARCHAR(30)</strong> long enough? Or maybe you should use <strong>VARCHAR(255)</strong> or maybe <strong>TEXT</strong> as I&#8217;ve seen in some projects?</p>
<p>My live database, which I&#8217;ve used for this test contains <strong>92298</strong> valid email addresses, verified with the <a href="http://pear.php.net/package/Validate" target="_blank">Pear Validate</a> class with domain checking.<br />
<span id="more-1"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>Validate<span style="color: #339933;">::</span><span style="color: #004000;">email</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// add to database</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// return error</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It means, if the email address is in this database &#8211; it is real and correct, otherwise the field is empty. So to make sure not to include empty fields, I&#8217;ve issued following query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">SIZE</span> <span style="color: #993333; font-weight: bold;">FROM</span> customer <span style="color: #993333; font-weight: bold;">WHERE</span> email <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%@%'</span> <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">SIZE</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">SIZE</span>;</pre></div></div>

<p>This query returns set of values, which I&#8217;ve used to create this chart (with <a title="OpenOffice.org" href="http://openoffice.org" target="_blank">OpenOffice.org</a> Calc).</p>
<p style="text-align: center;"><img class="size-full wp-image-12 aligncenter" title="Average emial address length" src="http://janusz.slota.name/blog/wp-content/uploads/2009/05/email-length-stats_html_54813cd4.jpg" alt="email-length-stats_html_54813cd4" width="815" height="241" /></p>
<p>As you can see majority of email addresses are around 13 to 34 characters long. To store email addresses I&#8217;ve used <strong>VARCHAR (255)</strong> but as you can see <strong>VARCHAR(50)</strong> is enough.</p>
<p>The answer: <strong>VARCHAR(50)</strong> should do.</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2009/05/email-length/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

