<?php

require_once 'model/om/BaseSnippetTag.php';


/**
 * Skeleton subclass for representing a row from the 'sn_tag' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package model
 */	
class SnippetTag extends BaseSnippetTag
{
  public function save($con = null)
  {
    $con = Propel::getConnection();
    try
    {
      $con->begin();

      $ret = parent::save($con);

      $this->updateRelatedSnippet($con);

      $con->commit();

      return $ret;
    }
    catch (Exception $e)
    {
      $con->rollback();
      throw $e;
    }
  }

  public function delete($con = null)
  {
    $con = Propel::getConnection();
    try
    {
      $con->begin();

      $this->updateRelatedSnippet($con);

      parent::delete($con);

      $con->commit();
    }
    catch (Exception $e)
    {
      $con->rollback();
      throw $e;
    }
  }

  // update all_tags in snippet table
  private function updateRelatedSnippet($con)
  {
    $snippet = $this->getSnippetSnippet();
    $c = new Criteria();
    $c->add(SnippetTagPeer::SNIPPET_ID, $snippet->getId());
    $c->addAscendingOrderByColumn(SnippetTagPeer::NAME);
    $tags = SnippetTagPeer::doSelect($c, $con);

    $all_tags = '';
    foreach($tags as $tag)
    {
      $all_tags .= $tag->getName().' ';
    }

    $snippet->setAllTags(trim($all_tags)); 
    $snippet->save($con);
  }
}

