You have to create the Listener :
<?php
class Doctrine_Template_Listener_Usertraceable extends
Doctrine_Record_Listener {
    /**
     *
     * @var string
     */
    protected $_options = array();

    /**
     * __construct
     *
     * @param string $options
     * @return void
     */
    public function __construct(array $options)
    {
        $this->_options = $options;
    }

    /**
     *
     * @param Doctrine_Event $event
     * @return void
     */
    public function preInsert(Doctrine_Event $event)
    {
     $guard_user = sfContext::getInstance()->getUser()->getGuardUser();
        if ( ! $this->_options['created']['disabled']) {
            $createdName =
$event->getInvoker()->getTable()->getFieldName($this->_options['created']['name']);
            $modified = $event->getInvoker()->getModified();
            if ( ! isset($modified[$createdName])) {
                $event->getInvoker()->$createdName = $guard_user;
            }
        }

        if ( ! $this->_options['updated']['disabled']) {
            $updatedName =
$event->getInvoker()->getTable()->getFieldName($this->_options['updated']['name']);
            $modified = $event->getInvoker()->getModified();
            if ( ! isset($modified[$updatedName])) {
                $event->getInvoker()->$updatedName = $guard_user;
            }
        }
    }

    /**
     *
     * @param Doctrine_Event $evet
     * @return void
     */
    public function preUpdate(Doctrine_Event $event)
    {
     $guard_user = sfContext::getInstance()->getUser()->getGuardUser();

        if ( ! $this->_options['updated']['disabled']) {
            $updatedName =
$event->getInvoker()->getTable()->getFieldName($this->_options['updated']['name']);
            $modified = $event->getInvoker()->getModified();
            if ( ! isset($modified[$updatedName])) {
                $event->getInvoker()->$updatedName = $guard_user;
            }
        }
    }

    /**
     * Set the updated field for dql update queries
     *
     * @param Doctrine_Event $evet
     * @return void
     */
    public function preDqlUpdate(Doctrine_Event $event)
    {
     $guard_user = sfContext::getInstance()->getUser()->getGuardUser();

        if ( ! $this->_options['updated']['disabled']) {
            $params = $event->getParams();
            $updatedName =
$event->getInvoker()->getTable()->getFieldName($this->_options['updated']['name']);
            $field = $params['alias'] . '.' . $updatedName;
            $query = $event->getQuery();

            if ( ! $query->contains($field)) {
                $query->set($field, '?', $guard_user);
            }
        }
    }

}
?>

2009/6/26 juro <fo...@juro.at>

>
> Hi,
> using the sfDoctrineGuardPlugin, I have created a new Doctrine
> behaviour/template (similar to the Timestampable behaviour). This
> creates two fields created_by and updated_by that *should* be foreign
> keys to the sfGuardUser.id field. Currently the behaviour creates the
> fields but not the relationships. Can anyone check why not?
>
> juro
>
>
> <?php
> /**
>  * Doctrine Template that creates two fields created_by and updated_by
>  * @author: Roland Jungwirth
>  * @version: 0.9
>  */
> class Doctrine_Template_Usertraceable extends Doctrine_Template
> {
>  /* Array of Timestampable options
>   *
>   * @var string
>   */
>  protected $_options = array('created' =>  array('name'          =>
> 'created_by',
>                                                  'type'          =>
> 'integer',
>                                                  'length'        =>
> 4,
>                                                  'disabled'      =>
> false,
>                                                  'options'       =>
> array()),
>                              'updated' =>  array('name'          =>
> 'updated_by',
>                                                  'type'          =>
> 'integer',
>                                                  'length'        =>
> 4,
>                                                  'disabled'      =>
> false,
>                                                  'options'       =>
> array()));
>
>  public function setTableDefinition()
>  {
>    if( ! $this->_options['created']['disabled']) {
>      $this->hasColumn($this->_options['created']['name'], $this-
> >_options['created']['type'], 4, $this->_options['created']
> ['options']);
>    }
>
>    if( ! $this->_options['updated']['disabled']) {
>      $this->hasColumn($this->_options['updated']['name'], $this-
> >_options['updated']['type'], 4, $this->_options['updated']
> ['options']);
>    }
>  }
>
>  public function setUp()
>  {
>    $this->hasOne('sfGuardUser as CreatedBy', array('local' => $this-
> >_options['created']['name'], 'foreign' => 'id'));
>    $this->hasOne('sfGuardUser as UpdatedBy', array('local' => $this-
> >_options['updated']['name'], 'foreign' => 'id'));
>  }
> }
> ?>
> >
>

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

Reply via email to