<?php

require_once 'model/om/BaseSnippetComment.php';


/**
 * Skeleton subclass for representing a row from the 'sn_comment' 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 SnippetComment extends BaseSnippetComment
{
  public function setBody($body)
  {
    parent::setBody($body);

    $this->setHtmlBody(myToolkit::transformToHtml($body));
  }

  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();

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

      $this->updateRelatedSnippet($con);

      $con->commit();

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

  // update nb_comments in snippet table
  private function updateRelatedSnippet($con)
  {
    $snippet = $this->getSnippetSnippet();
    $c = new Criteria();
    $c->add(SnippetCommentPeer::SNIPPET_ID, $snippet->getId());
    $snippet->setNbComments(SnippetCommentPeer::doCount($c, $con));
    $snippet->save($con);
  }
}

