/**
  * Performs the work of inserting or updating the row in the database.
  *
  * If the object is new, it inserts it; otherwise an update is performed.
  * All related objects are also updated in this method.
  *
  * @param      PropelPDO $con
  * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
  * @throws PropelException
  * @see        save()
  */
 protected function doSave(PropelPDO $con)
 {
     $affectedRows = 0;
     // initialize var to track total num of affected rows
     if (!$this->alreadyInSave) {
         $this->alreadyInSave = true;
         // We call the save method on the following object(s) if they
         // were passed to this object by their coresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aSystemEventInstance !== null) {
             if ($this->aSystemEventInstance->isModified() || $this->aSystemEventInstance->isNew()) {
                 $affectedRows += $this->aSystemEventInstance->save($con);
             }
             $this->setSystemEventInstance($this->aSystemEventInstance);
         }
         if ($this->aSystemEventSubscription !== null) {
             if ($this->aSystemEventSubscription->isModified() || $this->aSystemEventSubscription->isNew()) {
                 $affectedRows += $this->aSystemEventSubscription->save($con);
             }
             $this->setSystemEventSubscription($this->aSystemEventSubscription);
         }
         if ($this->isNew() || $this->isModified()) {
             // persist changes
             if ($this->isNew()) {
                 $this->doInsert($con);
             } else {
                 $this->doUpdate($con);
             }
             $affectedRows += 1;
             $this->resetModified();
         }
         $this->alreadyInSave = false;
     }
     return $affectedRows;
 }
 /**
  * Triggers a system event. 
  * 
  * This will create a SystemEventInstance instance and notify any remote 
  * systems if they have active SystemEventSubscription objects. 
  * 
  * Each subscriber will be sent a unique SystemEventInstanceMessage.
  * 
  * eg.
  * 
  *     $message = new stdClass();
  *     $message->user_id = 54;
  *     $message->city = 'Vancouver';
  *     \SystemEventPeer::triggerEvent( 'new_user_signed_up', $message );
  * 
  * 
  * 
  * @param string $event_unique_key
  * @param stdClass $message
  * @param integer $user_id
  * 
  * @throws \Exception if system event is not known
  * @throws \Exception if $message is not a stdClass (if not null)
  * @throws \Exception if $user_id is provided (not null) but not found
  * 
  * @return SystemEventInstance
  */
 public static function triggerEvent($event_unique_key, $message = null, $user_id = null)
 {
     //validate the arguments
     $system_event = \SystemEventPeer::retrieveByUniqueKey($event_unique_key);
     if (!$system_event) {
         throw new \Exception('Unknown System Event: ' . $event_unique_key);
     }
     if (!is_null($message)) {
         if (!$message instanceof \stdClass) {
             throw new \Exception('Message must be a stdClass.');
         }
     } else {
         $message = new \stdClass();
     }
     if (!is_null($user_id)) {
         $user = \UserPeer::retrieveByPK($user_id);
         if (!$user) {
             throw new \Exception('Unknown User.');
         }
     } else {
         $user = \sfContext::getInstance()->getUser()->getProfile();
         if (!$user) {
             throw new \Exception('User must be logged in or you must provide a user_id to triggerError()');
         }
         $user_id = $user->getId();
     }
     //record the event
     $system_event_instance = new \SystemEventInstance();
     $system_event_instance->setMessage(json_encode($message));
     if (isset($user)) {
         $system_event_instance->setUser($user);
     }
     $system_event_instance->setSystemEvent($system_event);
     $system_event_instance->save();
     //get the subscribers for this event
     $system_event_subscriptions = \SystemEventSubscriptionPeer::getSubscriptionsForEvent($system_event->getId(), $user_id);
     //notify each of the subscribers
     foreach ($system_event_subscriptions as $system_event_subscription) {
         $system_event_subscription->saveSystemEventNotification($system_event_instance);
     }
 }