<?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>Rob Rosenbaum's Development Blog &#187; symfony</title>
	<atom:link href="http://robrosenbaum.com/tags/symfony/feed/" rel="self" type="application/rss+xml" />
	<link>http://robrosenbaum.com</link>
	<description>PHP, Symfony, and Other Web Things</description>
	<lastBuildDate>Wed, 30 Jan 2008 02:38:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HOWTO: Disable Session Timeout in Symfony</title>
		<link>http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/</link>
		<comments>http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 20:10:07 +0000</pubDate>
		<dc:creator>Rob Rosenbaum</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/</guid>
		<description><![CDATA[The problem: make symfony use PHP's default session behavior. Symfony session handling is based on timed sessions - you set a time after which the session will expire - but the default PHP behavior is to expire when the user closes the browser window. This is also the most commonly desired behavior for sessions. The [...]]]></description>
			<content:encoded><![CDATA[<p>The problem: make symfony use PHP's default session behavior. Symfony session handling is based on timed sessions - you set a time after which the session will expire - but the default PHP behavior is to expire when the user closes the browser window. This is also the most commonly desired behavior for sessions. The drawback of using a timed session is that it could expire while the user is still on the site. This is an issue in <a href="http://www.lecturetools.org/">LectureTools</a>, because students may sit for an hour or more without loading a new page, and they may even have logged in before class. You can solve this little problem easily if you are using symfony 1.1 (currently in beta). Just set the timeout to <code>false</code> in settings.yml:</p>
<pre class="YAML">
all:
  .settings:
    timeout:    false
</pre>

But if you are using the stable 1.0 branch of symfony, this will do just the opposite - it will make your sessions time out immediately! To make this work, we have to override <code>sfBasicSecurityUser</code>'s <code>initialize()</code> method. I will describe two ways to do this; one is a short hack, the other is longer and somewhat more proper.
<p class="leadingnoindent">Here's the short way:</p>
<pre class="PHP">
class myUser extends sfBasicSecurityUser
{
  public function initialize($context, $parameters = null)
  {
    if (sfConfig::get('sf_timeout') == 0) {
      // session will expire if window is open for a day
      sfConfig::set('sf_timeout', 86400);
    }

    return parent::initialize($context, $parameters);
  }
}
</pre>
This works because the <code>sf_timeout</code> setting is not used by any other class (at the moment), so changing it doesn't have any side effects. If you set your timeout to 0 or false, symfony will always think you have set the session timeout to 24 hours, but the session will actually expire when the user closes the browser window.
If you are not comfortable with "hacks" like the above, you could copy <code>the initialize()</code> method from <code>sfBasicSecurityUser</code>, change it in the appropriate place, and skip calling the parent constructor altogether. Unfortunately, <code>sfBasicSecurityUser</code> calls <em>its</em> parent constructor, so you have to rewrite them both:
<pre class="php">
class myUser extends sfBasicSecurityUser
{
  public function initialize($context, $parameters = null)
  {
    $this->context = $context;

    $this->parameterHolder = new sfParameterHolder();
    $this->parameterHolder->add($parameters);

    $this->attributeHolder = new sfParameterHolder(self::ATTRIBUTE_NAMESPACE);

    // read attributes from storage
    $attributes = $context->getStorage()->read(self::ATTRIBUTE_NAMESPACE);
    if (is_array($attributes))
    {
      foreach ($attributes as $namespace => $values)
      {
        $this->attributeHolder->add($values, $namespace);
      }
    }

    // set the user culture to sf_culture parameter if present in the request
    // otherwise
    //  - use the culture defined in the user session
    //  - use the default culture set in i18n.yml
    if (!($culture = $context->getRequest()->getParameter('sf_culture')))
    {
      if (null === ($culture = $context->getStorage()->read(self::CULTURE_NAMESPACE)))
      {
        $culture = sfConfig::get('sf_i18n_default_culture', 'en');
      }
    }

    $this->setCulture($culture);

    // read data from storage
    $storage = $this->getContext()->getStorage();

    $this->authenticated = $storage->read(self::AUTH_NAMESPACE);
    $this->credentials   = $storage->read(self::CREDENTIAL_NAMESPACE);
    $this->lastRequest   = $storage->read(self::LAST_REQUEST_NAMESPACE);

    if ($this->authenticated == null)
    {
      $this->authenticated = false;
      $this->credentials   = array();
    }
    else
    {
      // Automatic logout logged in user if no request within [sf_timeout] setting
      if (0 != sfConfig::get('sf_timeout') &#038;& null !== $this->lastRequest &#038;& (time() - $this->lastRequest) > sfConfig::get('sf_timeout'))
      {
        if (sfConfig::get('sf_logging_enabled'))
        {
          $this->getContext()->getLogger()->info('{sfUser} automatic user logout due to timeout');
        }
        $this->setTimedOut();
        $this->setAuthenticated(false);
      }
    }

    $this->lastRequest = time();

  }
}
</pre>

That should do it!]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/howto-disable-session-timeout-in-symfony/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>sfModelTestPlugin &#8211; Simple unit tests for any ORM!</title>
		<link>http://robrosenbaum.com/php/sfmodeltestplugin-simple-unit-tests-for-any-orm/</link>
		<comments>http://robrosenbaum.com/php/sfmodeltestplugin-simple-unit-tests-for-any-orm/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 19:02:31 +0000</pubDate>
		<dc:creator>Rob Rosenbaum</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/uncategorized/sfmodeltestplugin-simple-unit-tests-for-any-orm/</guid>
		<description><![CDATA[Easy unit testing with model objects and a test database with sfModelTest plugin. Propel, Doctrine, and Propel 1.3 are supported.]]></description>
			<content:encoded><![CDATA[<p>I have redone the <a href="http://trac.symfony-project.com/wiki/sfPropelTestPlugin">sfPropelTestPlugin</a>, and it is now <a href="http://trac.symfony-project.com/wiki/sfModelTestPlugin">sfModelTestPlugin</a>, with support for Doctrine and Propel 1.3! Also new - you can now specify the test data file/directory at runtime, allowing for more flexibility with your tests. I want to thank Anders Betnér and my readers for bug reports and patches!</p>]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/sfmodeltestplugin-simple-unit-tests-for-any-orm/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>New symfony plugin &#8211; sfPropelTestPlugin simplifies unit tests</title>
		<link>http://robrosenbaum.com/php/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/</link>
		<comments>http://robrosenbaum.com/php/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 12:59:47 +0000</pubDate>
		<dc:creator>Rob Rosenbaum</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/symfony/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/</guid>
		<description><![CDATA[Easy unit testing with Propel objects and a test database with sfPropelTestPlugin.]]></description>
			<content:encoded><![CDATA[<p>If you've ever tried to write a symfony unit test for a model class - or for any piece of code that interacts with the database - then you know what a headache it can be. <a href="http://trac.symfony-project.com/trac/wiki/sfPropelTestPlugin">sfPropelTestPlugin</a> to the rescue! This plugin loads all the necessary symfony components for database interaction, and goes a step further: test data is automatically reloaded to the test database at the beginning of each test case, so you don't have to worry about your tests interacting with each other. Here's an example of a unit test written with <a href="http://trac.symfony-project.com/trac/wiki/sfPropelTestPlugin">sfPropelTestPlugin</a>:</p>
<pre class="php">
class myUnitTest extends sfPropelTest
{
  public function setup()
  {
    $this->bob = UserPeer::retrieveByPK('bob');
  }

  public function teardown()
  {
    $this->diag('Test method complete!');
  }

  public function test_user()
  {
    $this->is($this->bob->getId(), 'bob', 'Bob exists!');

    $user = new User();
    $user->setFirstName('Joe');
    $user->setLastName('Smith');
    $user->setUsername('joe');
    $user->save();

    $joe = UserPeer::getBy(UserPeer::USERNAME, 'joe');
    $this->isa_ok($joe, 'User', 'Joe exists!');
  }

  public function test_dataDelete()
  {
    $this->is($this->bob->getId(), 'bob', 'Bob still exists!');

    $joe = UserPeer::getBy(UserPeer::USERNAME, 'joe');
    $this->ok(!$joe, 'Joe no longer exists.');
  }
}

$test = new myUnitTest();
$test->execute();
</pre>

<p class="leadingnoindent">If you've written tests in Ruby on Rails, the above should look familiar. If not, let's go through the code:</p>
<p>The first two lines are setup: The first line tells symfony what application to test - in this case, "myApp" - and the second line includes the code we need to test against the database. Next we define our "test case" - a class that extends <code class="php">sfPropelTest</code>, which is itself a child of <code class="php">lime_test</code>. Every method that begins with "test_" will be called in turn, with our test data being reloaded between each call. Also, the <code class="php">setup()</code> method, if defined, will be called immediately before each test method, and the <code class="php">teardown()</code> method, if defined, will be called immediately after. The last two lines create a new instance of our test suite, and call its <code class="php">execute()</code> method, which runs our tests.</p>
<p>There you have it - a quick and painless way to run unit tests in symfony. One less excuse for not writing them!</p>]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/new-symfony-plugin-sfpropeltestplugin-simplifies-unit-tests/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Improved Plugin for Model Validation: sfPropelValidatePlugin</title>
		<link>http://robrosenbaum.com/php/improved-plugin-for-model-validation-sfpropelvalidateplugin/</link>
		<comments>http://robrosenbaum.com/php/improved-plugin-for-model-validation-sfpropelvalidateplugin/#comments</comments>
		<pubDate>Mon, 10 Sep 2007 01:08:37 +0000</pubDate>
		<dc:creator>Rob Rosenbaum</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/symfony/improved-plugin-for-model-validation-sfpropelvalidateplugin/</guid>
		<description><![CDATA[Put your Symfony validation in the model, where it belongs, with sfPropelValidate plugin. New version is more efficient.]]></description>
			<content:encoded><![CDATA[<p>I have updated my plugin for doing <a href="http://trac.symfony-project.com/trac/wiki/sfPropelValidatePlugin">model-based validation in symfony</a>. The new plugin uses the Propel builders instead of behaviors, so it is now called <a href="http://trac.symfony-project.com/trac/wiki/sfPropelValidatePlugin">sfPropelValidatePlugin</a> instead of the former (unwieldy) name sfPropelValidateBehaviorPlugin. If you have not investigated the Propel builder classes, I suggest you give them a look. By overriding the default classes used to build your Propel object and peer classes, you can write your modifications directly into your model's base classes. This is a much cleaner and more readable solution than using Propel behaviors, and you avoid the performance hit that behaviors incur. </p>
<p>The default Propel builders can be found in the symfony source in these places:</p>
<pre class="filesystem">
symfony/addon/propel/builder/sfObjectBuilder.php
symfony/addon/propel/builder/sfPeerBuilder.php
</pre>
<p class="leadingnoindent">The classes these inherit from are buried. They can be found here:</p>
<pre class="filesystem">
symfony/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5ComplexObjectBuilder.php
symfony/vendor/propel-generator/classes/propel/engine/builder/om/php5/PHP5ComplexPeerBuilder.php
</pre>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/improved-plugin-for-model-validation-sfpropelvalidateplugin/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using Flash Upload with PHP &amp; symfony</title>
		<link>http://robrosenbaum.com/php/using-flash-upload-with-php-symfony/</link>
		<comments>http://robrosenbaum.com/php/using-flash-upload-with-php-symfony/#comments</comments>
		<pubDate>Tue, 04 Sep 2007 22:50:09 +0000</pubDate>
		<dc:creator>Rob Rosenbaum</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/flash/using-flash-upload-with-php-symfony/</guid>
		<description><![CDATA[Using Flash-based multi-file uploads with session cookies.]]></description>
			<content:encoded><![CDATA[<p>Through Flash's <a href="http://livedocs.adobe.com/flex/201/langref/flash/net/FileReference.html"><code class="ActionScript">FileReference</code></a> and <a href="http://livedocs.adobe.com/flex/201/langref/flash/net/FileReferenceList.html"><code class="ActionScript">FileReferenceList</code></a> classes, you  can create a powerful file uploader that allows the user to upload multiple files with a single form element. But beware: unless the user happens to be using IE, the <code class="ActionScript">upload</code> will use a new browser session to upload the files. This means that if you require that a user be authenticated before uploading something (and you better be), the upload won't work - the request will be forwarded to the login form, or to wherever your system forwards unauthorized requests. This is a maddening bug to track down, and there is nothing you can do to make Flash use the right session. The work around is to send the session cookie in the url and, on the server side, use that to override the new (and wrong) session cookie sent by Flash. Here's how that works in symfony, although much of the following is useful for other languages or frameworks:</p>
<p>First, we have to tell our Flash component what the cookie is, so it can roll it into the URL. One solution would be to pass the session cookie as an argument in <a href="http://www.permadi.com/tutorial/flashVars/">FlashVars</a>, but then the user's session cookie is sitting unencrypted in the HTML, leaving them vulnerable to <a href="http://shiflett.org/articles/cross-site-request-forgeries">cross-site request forgery</a>. Better to use javascript to fetch the cookie, and <em>then</em> put it in FlashVars. If you are using <a href="http://www.bobbyvandersluis.com/ufo/">ufo.js</a>, the first argument to <code class="javascript">UFO.create()</code> should look like this:</p>
<pre class="javascript">
{
  movie: '/flash/uploader.swf', 
  id: 'uploader',
  name: 'uploader',

  flashvars: 'cookie=' + document.cookie,

  // other options ...
}
</pre>
<p>Note that if you are dealing with multilple cookies, you will want to parse the output of <code class="javascript">document.cookie</code> and send only the desired cookie.</p>
<p class="leadingnoindent">Next, add the cookie to the URL in your ActionScript (this is for ActionScript 2.0):</p>
<pre class="ActionScript2">
var list:Array = myFileRefList.fileList;
var item:FileReference;
var url:String = _root.uploadURL + '?cookie=' + _root.cookie;

for (var i:Number = 0; i < list.length; i++) {
  item.upload (url);
}
</pre>
<p class="leadingnoindent">That does it for the client side. Now, we need to tell PHP to use our cookie instead of the one the browser sent. This does the trick:</p>
<pre class="PHP">
list($cookieName, $cookieValue) = str_split('=', $_GET['cookie']);

session_name($cookieName);
session_id($cookieValue);

session_start();
</pre>
<p>If you are using symfony, things are a little more complex. We need to call <code class="PHP">session_name()</code> and <code class="PHP">session_id()</code> <em>before</em> <code class="PHP">session_start()</code>; looking at the symfony source code, we find that <code class="PHP">session_start()</code> is called in <code class="PHP">sfSessionStorage::initialize()</code>. So a simple solution is to extend the sfSessionStorage class:</p>
<pre class="PHP">
class mySessionStorage extends sfSessionStorage
{
  public function initialize($context, $parameters = null)
  {
    if ( /* whatever the condition is when we want to do this */ ) {
      if ($cookie = $context->getRequest()->getParameter('cookie')) {
        $name = 'symfony';
        preg_match('/^' . $name.'=(.*)$/', $cookie, $asMatch);
        $value = $asMatch[1];

        session_name($name);
        session_id($value);
      }
    }

    parent::initialize($context, $parameters);
  }
}
</pre>
<p class="leadingnoindent">Finally, tell symfony to use the mySessionStorage class by editting factories.yml:</p>
<pre class="YAML">
all:
  storage:
    class: mySessionStorage
    param:
      session_name: symfony
</pre>
<p class="leadingnoindent">And you're done! Clear your cache, and try it out. An easy way to check if it's working is to add the following line to the end of index.php (or frontend.php):</p>
<pre class="PHP">file_put_contents('testSessionOverwrite.txt', sfContext::getInstance()->getRequest()->getActionName());</pre>
<p>Now try uploading a file. If the action name in testSessionOverwrite.txt is the action you're uploading to, you're golden. If instead it is the name of the action that authenticates users, you have a problem somewhere, and you get to have the enjoyable experience of debugging PHP without browser output. Remember to erase the debugging line from your front controller when you get everything working.</p>
<p>Next time, just make the users upload their damn files one-at-a-time...</p>
]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/php/using-flash-upload-with-php-symfony/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Validate the data, not the form</title>
		<link>http://robrosenbaum.com/symfony/validate-the-data-not-the-form/</link>
		<comments>http://robrosenbaum.com/symfony/validate-the-data-not-the-form/#comments</comments>
		<pubDate>Sun, 03 Jun 2007 18:07:15 +0000</pubDate>
		<dc:creator>Rob Rosenbaum</dc:creator>
				<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://robrosenbaum.com/?p=5</guid>
		<description><![CDATA[Put your Symfony validation logic in the model, where it belongs, with sfPropelValidateBehavior plugin.]]></description>
			<content:encoded><![CDATA[<p class="aside">I recently wrote a <a href="http://trac.symfony-project.com/trac/wiki/sfPropelValidateBehaviorPlugin">model-validation plugin for symfony</a>. I believe this plugin, or something like it, should be included in the symfony core. Here's why.</p>
<p>
In an <a href="http://en.wikipedia.org/wiki/Model-view-controller">MVC architecture</a>, the model layer (the M in "MVC") encapsulates the data model of our application. This includes not only data storage and access, but also validation - that is, the data model should specify what data is allowed, and what is forbidden. In symfony, for example, the save() method of every data object should first validate the object. Thus, in a strict MVC architecture, we should perform only form-specific validation in the controller. But in a web application, since almost all data that needs to be validated comes from web forms, we often "cheat" by validating the form data in the controller layer <em>instead</em> of validating within the model. symfony does things this way. While there are several efficient ways of <a href="http://www.symfony-project.com/book/trunk/10-Forms">validating forms in symfony</a>, they all occur between the end-user's submission of a form, and the corresponding "update" action. Thus, symfony provides support only for <em>controller-based</em> validation. To be fair, we can validate within the model, by overriding the validate() method of each data class in our model. But this represents a lot of repeated code. Furthermore, if we want to use YAML files to specify validation, or if we want to set errors that can be accessed from the presentation layer, we must manually call functions to accomplish these things.</p>
<p>It would be better if we could use symfony's automated validation logic from within the model. This would not only allow, but <em>encourage</em> developers to move validation related to the data model out of the controller and into the model. Doing so not only helps us understand our application conceptually; it also protects our data from bugs in the presentation layer. As developers, we are wise to be skeptical of ourselves, especially when the security of our data may depend on it. Encapsulation can also protect us from <a href="http://trac.symfony-project.com/trac/ticket/1617">bugs  in the framework itself, like this one</a>.</p>
<p>For the application I am currently developing, data security is the top priority. So I wrote the <a href="http://trac.symfony-project.com/trac/wiki/sfPropelValidateBehaviorPlugin">sfPropelValidateBehavior plugin</a>. Now you can associate your validation logic with your data classes and put it in your project's "model" directory. In most cases, you can copy the configuration from you form-validation files as-is. I hope this helps you other symfony developers!</p>]]></content:encoded>
			<wfw:commentRss>http://robrosenbaum.com/symfony/validate-the-data-not-the-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
