Hi,

I wrote a behavior plugin to do that for my application, because I had
to do that for multiple objects. It's actually quite simple. But I
didn't package and publish it.
You'll probably have to modify it slightly for symfony 1.2 though; it
was written for symfony 1.0. Is sfConfig still there? I'm not up to date
there... But I think you'll get the idea.

So I created two files:
plugins/sfPropelModifiedByUserBehaviorPlugin/lib/sfPropelModifiedByUserBehavior.class.php:
<?php
/**
 * saves user who created or modified an object
 *
 * @author Martin Kreidenweis
 */
class sfPropelModifiedByUserBehavior
{
  private static $userId = null;

  public function preSave(BaseObject $object)
  {
    $class = get_class($object);

    if ($object->isNew())
    {
      $columnName =
sfConfig::get('propel_behavior_sfPropelModifiedByUserBehavior_'.$class.'_created_column',
'created_by_user_id');
    }
    else if ($object->isModified())
    {
      $columnName =
sfConfig::get('propel_behavior_sfPropelModifiedByUserBehavior_'.$class.'_updated_column',
'updated_by_user_id');
    }
    else
    {
      return;
    }

    $methodPostfix = call_user_func(array($class.'Peer',
                                          'translateFieldName'),
                                    $columnName,
                                    BasePeer::TYPE_FIELDNAME,
                                    BasePeer::TYPE_PHPNAME);
    $setMethod = 'set'.$methodPostfix;

    // you could check here if the column has already been modified
    // and only set it if not, to still be able to manually set it

    $object->$setMethod(self::$userId);
  }

  /**
   * Sets the userId the behavior should use to mark modified objects
   * @param int $userId
   */
  public static function setUserId($userId)
  {
    self::$userId = $userId;
  }

  public static function getUserId()
  {
    return self::$userId;
  }
}


I'm quite sure the second file has to be different for symfony 1.2, this
is how I registered my behavior in 1.0:
plugins/sfPropelModifiedByUserBehaviorPlugin/config/config.php:
<?php
sfPropelBehavior::registerHooks('sfPropelModifiedByUserBehavior', array(
':save:pre'      => array('sfPropelModifiedByUserBehavior', 'preSave'),
));

Check the documentation for the correct way to register a behavior in
later symfony versions.


Then somewhere in your code you have to pass down the current user ID to
the ORM layer: sfPropelModifiedByUserBehavior::setUserId($userId);
You can for example override the initialize() method of your sfUser
descendant to do that on every request. So you won't have to modify
every single action that touches an object.

With this done now you can simply configure for which objects the
behavior should be used by adding the following line at the end of your
lib/model/YourObjectClassName.php file:
sfPropelBehavior::add('YourObjectClassName',array('sfPropelModifiedByUserBehavior'));

You can also pass custom field names if you need to:
sfPropelBehavior::add('EmployeeComplaint', array(
  'sfPropelModifiedByUserBehavior' =>
    array('created_column' => 'created_by_user_id',
          'updated_column' => 'last_processed_by_user_id'))
);

It takes some work up front. But it pays off if you have to do that
stuff for multiple objects I think. And you have all the code taking
care of that concern in one place (DRY principle).
By passing the user ID down from the controller layer to the ORM layer
and not reaching up you keep the layer separation.

Hope that helps,
Martin


Tomasz Ignatiuk schrieb:
> I use symfony 1.2.3, Propel. Something more?
> 
> 2009/2/2 Łukasz Wojciechowski <[email protected]>
> 
> 
>     Hi
> 
>     You should give us more info.
> 
>     Most important is which ORM engine You use? Propel or Doctrine ?
> 
>     --
>     Best regards
>     Łukasz Wojciechowski
> 
>     New Generation Software
>     +48 602 214 629
> 
> 
> 
>     2009/2/2 Tomasz Ignatiuk <[email protected]
>     <mailto:[email protected]>>:
>     >
>     > Hi
>     >
>     > In almost every table in my application I have a field called:
>     > login_modified which should keep login of a user who just updated a
>     > record (or created). It is similar to updated_at but "by who". I keep
>     > login in session. I'd like to ask you how to put login into
>     > login_modified input in almost all forms in a project? Do I have to
>     > call new form object (witch second parameter as option) in each
>     > executeNew (executeEdit) action? Or there is a better way?
>     > >
>     >
>     >
> 
> 
> 
> 
> > 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to