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.

1 комментарий:

Анонимный комментирует...

I got round this by creating a configuration handler class to read in my propel behaviours. This is then loaded in using a custom filter in the filter chain.

The propel behaviours are loaded each request and the config is cached in applications. This does mean that you'd have to set it up per application but in my case this is perfect.