2009-02-26

Proper Propel behavior registration

Suppose you add a propel behavior using this code:
sfPropelBehavior::add('Article', array(
 'positioned'
));
Now, where to put it? The cookbook suggests it should be put "in lib/model/Article.php". But what if your behavior includes hooks for peer classes, and in a certain action you call peer methods before using model classes (which is rather common)? In this case you might end up without any behavior attached before the first call of any peer method.

This happens because of symfony autoloading mechanism:
  1. Peer class method is called
  2. It is not found, so autoloading steps in
  3. Peer class is loaded
  4. Peer class method is executed
  5. Model class is loaded
  6. Propel behavior is added
  7. Result set is hyrdated
  8. ...
You see: because model class is loaded after the peer class' method executes, there is no behavior attached to peer class at the time of method execution.

Registering behaviors in projectConfiguration doesn't work.

The only solution I found is to register them in plugin's config.php.

Propel Behaviors: registering hooks for doSelectRS

If you read the chapter of symfony cookbook which talks about Propel behaviors, and tried to register a hook for doSelectRS, you'd find out it doesn't work.

In fact, doSelectRS was removed as of symfony 1.2. doSelectStmt took its place, so if you want to add a hook to selecting records, you should write something like this:
sfPropelBehavior::registerHooks('positioned', array(
 'Peer:doSelectStmt:doSelectStmt' => array('wgPropelPositionedBehavior', 'addAscendingOrderByPosition'),
));

2009-02-25

How to get all action variables

For my programming work, I use symfony framework. It has a funny system of passing variables from actions to templates: you just set the variable as a property of your action object and it automatically becomes available in template that is used to render results. So, there is a bunch of variables you can use, but no way of getting the whole array of them. This line will do the trick:
$vars = $sf_context->getActionStack()->getLastEntry()->getActionInstance()->getVarHolder()->getAll();

2009-02-01

Mouse wheel handling with Prototype

There is a bug wandering the web. If you google for "prototype mouse wheel", you'll find some snippets of code. They are rather good, except for their age. Those are '06-'07 oldies, and we have '09 around. Since the time that code was written, there has been a release of Firefox 3, in which they fixed one tiny wee peculiarity: the value of event detail property, which holds the "amount" of scroll. Before the new version of FF it was +-3 for single wheel turn, which is reflected in the old code as the division of detail by 3, which gives "normalized" wheel move data. FF3 guys thought it was a shame to have 3 as value of detail property while in fact it represents only one wheel turn. So they adjusted it to be nice +-1. The problem is, old code returns zero now, because 1/3 equals 0. I will post my fix for this solution, although it's easy as heck to port it to other one.
delta = !!event.detail*(event.detail>0? 1 : -1);
Now this behaves good both in FF2 and FF3.