<?php


abstract class BaseTblblogpoll extends BaseObject  implements Persistent {


	
	protected static $peer;

	
	protected $poll_id;

	
	protected $poll_name;

	
	protected $poll_questions;

	
	protected $poll_type;

	
	protected $poll_blog;

	
	protected $poll_createdate;

	
	protected $poll_active;

	
	protected $aTblblog;

	
	protected $collTblblogpolldatas;

	
	private $lastTblblogpolldataCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		parent::__construct();
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
	}

	
	public function getPollId()
	{
		return $this->poll_id;
	}

	
	public function getPollName()
	{
		return $this->poll_name;
	}

	
	public function getPollQuestions()
	{
		return $this->poll_questions;
	}

	
	public function getPollType()
	{
		return $this->poll_type;
	}

	
	public function getPollBlog()
	{
		return $this->poll_blog;
	}

	
	public function getPollCreatedate($format = 'Y-m-d H:i:s')
	{
		if ($this->poll_createdate === null) {
			return null;
		}


		if ($this->poll_createdate === '0000-00-00 00:00:00') {
									return null;
		} else {
			try {
				$dt = new DateTime($this->poll_createdate);
			} catch (Exception $x) {
				throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->poll_createdate, true), $x);
			}
		}

		if ($format === null) {
						return $dt;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $dt->format('U'));
		} else {
			return $dt->format($format);
		}
	}

	
	public function getPollActive()
	{
		return $this->poll_active;
	}

	
	public function setPollId($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->poll_id !== $v) {
			$this->poll_id = $v;
			$this->modifiedColumns[] = TblblogpollPeer::POLL_ID;
		}

		return $this;
	} 
	
	public function setPollName($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->poll_name !== $v) {
			$this->poll_name = $v;
			$this->modifiedColumns[] = TblblogpollPeer::POLL_NAME;
		}

		return $this;
	} 
	
	public function setPollQuestions($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->poll_questions !== $v) {
			$this->poll_questions = $v;
			$this->modifiedColumns[] = TblblogpollPeer::POLL_QUESTIONS;
		}

		return $this;
	} 
	
	public function setPollType($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->poll_type !== $v) {
			$this->poll_type = $v;
			$this->modifiedColumns[] = TblblogpollPeer::POLL_TYPE;
		}

		return $this;
	} 
	
	public function setPollBlog($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->poll_blog !== $v) {
			$this->poll_blog = $v;
			$this->modifiedColumns[] = TblblogpollPeer::POLL_BLOG;
		}

		if ($this->aTblblog !== null && $this->aTblblog->getBlId() !== $v) {
			$this->aTblblog = null;
		}

		return $this;
	} 
	
	public function setPollCreatedate($v)
	{
						if ($v === null || $v === '') {
			$dt = null;
		} elseif ($v instanceof DateTime) {
			$dt = $v;
		} else {
									try {
				if (is_numeric($v)) { 					$dt = new DateTime('@'.$v, new DateTimeZone('UTC'));
															$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
				} else {
					$dt = new DateTime($v);
				}
			} catch (Exception $x) {
				throw new PropelException('Error parsing date/time value: ' . var_export($v, true), $x);
			}
		}

		if ( $this->poll_createdate !== null || $dt !== null ) {
			
			$currNorm = ($this->poll_createdate !== null && $tmpDt = new DateTime($this->poll_createdate)) ? $tmpDt->format('Y-m-d H:i:s') : null;
			$newNorm = ($dt !== null) ? $dt->format('Y-m-d H:i:s') : null;

			if ( ($currNorm !== $newNorm) 					)
			{
				$this->poll_createdate = ($dt ? $dt->format('Y-m-d H:i:s') : null);
				$this->modifiedColumns[] = TblblogpollPeer::POLL_CREATEDATE;
			}
		} 
		return $this;
	} 
	
	public function setPollActive($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->poll_active !== $v) {
			$this->poll_active = $v;
			$this->modifiedColumns[] = TblblogpollPeer::POLL_ACTIVE;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array())) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->poll_id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
			$this->poll_name = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
			$this->poll_questions = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->poll_type = ($row[$startcol + 3] !== null) ? (int) $row[$startcol + 3] : null;
			$this->poll_blog = ($row[$startcol + 4] !== null) ? (int) $row[$startcol + 4] : null;
			$this->poll_createdate = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
			$this->poll_active = ($row[$startcol + 6] !== null) ? (int) $row[$startcol + 6] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 7; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Tblblogpoll object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aTblblog !== null && $this->poll_blog !== $this->aTblblog->getBlId()) {
			$this->aTblblog = null;
		}
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(TblblogpollPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = TblblogpollPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aTblblog = null;
			$this->collTblblogpolldatas = null;
			$this->lastTblblogpolldataCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(TblblogpollPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			TblblogpollPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(TblblogpollPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			$con->commit();
			TblblogpollPeer::addInstanceToPool($this);
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aTblblog !== null) {
				if ($this->aTblblog->isModified() || $this->aTblblog->isNew()) {
					$affectedRows += $this->aTblblog->save($con);
				}
				$this->setTblblog($this->aTblblog);
			}

			if ($this->isNew() ) {
				$this->modifiedColumns[] = TblblogpollPeer::POLL_ID;
			}

						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = TblblogpollPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setPollId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += TblblogpollPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			if ($this->collTblblogpolldatas !== null) {
				foreach ($this->collTblblogpolldatas as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aTblblog !== null) {
				if (!$this->aTblblog->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aTblblog->getValidationFailures());
				}
			}


			if (($retval = TblblogpollPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collTblblogpolldatas !== null) {
					foreach ($this->collTblblogpolldatas as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = TblblogpollPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		$field = $this->getByPosition($pos);
		return $field;
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getPollId();
				break;
			case 1:
				return $this->getPollName();
				break;
			case 2:
				return $this->getPollQuestions();
				break;
			case 3:
				return $this->getPollType();
				break;
			case 4:
				return $this->getPollBlog();
				break;
			case 5:
				return $this->getPollCreatedate();
				break;
			case 6:
				return $this->getPollActive();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = TblblogpollPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getPollId(),
			$keys[1] => $this->getPollName(),
			$keys[2] => $this->getPollQuestions(),
			$keys[3] => $this->getPollType(),
			$keys[4] => $this->getPollBlog(),
			$keys[5] => $this->getPollCreatedate(),
			$keys[6] => $this->getPollActive(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = TblblogpollPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setPollId($value);
				break;
			case 1:
				$this->setPollName($value);
				break;
			case 2:
				$this->setPollQuestions($value);
				break;
			case 3:
				$this->setPollType($value);
				break;
			case 4:
				$this->setPollBlog($value);
				break;
			case 5:
				$this->setPollCreatedate($value);
				break;
			case 6:
				$this->setPollActive($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = TblblogpollPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setPollId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setPollName($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setPollQuestions($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setPollType($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setPollBlog($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setPollCreatedate($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setPollActive($arr[$keys[6]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(TblblogpollPeer::DATABASE_NAME);

		if ($this->isColumnModified(TblblogpollPeer::POLL_ID)) $criteria->add(TblblogpollPeer::POLL_ID, $this->poll_id);
		if ($this->isColumnModified(TblblogpollPeer::POLL_NAME)) $criteria->add(TblblogpollPeer::POLL_NAME, $this->poll_name);
		if ($this->isColumnModified(TblblogpollPeer::POLL_QUESTIONS)) $criteria->add(TblblogpollPeer::POLL_QUESTIONS, $this->poll_questions);
		if ($this->isColumnModified(TblblogpollPeer::POLL_TYPE)) $criteria->add(TblblogpollPeer::POLL_TYPE, $this->poll_type);
		if ($this->isColumnModified(TblblogpollPeer::POLL_BLOG)) $criteria->add(TblblogpollPeer::POLL_BLOG, $this->poll_blog);
		if ($this->isColumnModified(TblblogpollPeer::POLL_CREATEDATE)) $criteria->add(TblblogpollPeer::POLL_CREATEDATE, $this->poll_createdate);
		if ($this->isColumnModified(TblblogpollPeer::POLL_ACTIVE)) $criteria->add(TblblogpollPeer::POLL_ACTIVE, $this->poll_active);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(TblblogpollPeer::DATABASE_NAME);

		$criteria->add(TblblogpollPeer::POLL_ID, $this->poll_id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getPollId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setPollId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setPollName($this->poll_name);

		$copyObj->setPollQuestions($this->poll_questions);

		$copyObj->setPollType($this->poll_type);

		$copyObj->setPollBlog($this->poll_blog);

		$copyObj->setPollCreatedate($this->poll_createdate);

		$copyObj->setPollActive($this->poll_active);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getTblblogpolldatas() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addTblblogpolldata($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setPollId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new TblblogpollPeer();
		}
		return self::$peer;
	}

	
	public function setTblblog(Tblblog $v = null)
	{
		if ($v === null) {
			$this->setPollBlog(NULL);
		} else {
			$this->setPollBlog($v->getBlId());
		}

		$this->aTblblog = $v;

						if ($v !== null) {
			$v->addTblblogpoll($this);
		}

		return $this;
	}


	
	public function getTblblog(PropelPDO $con = null)
	{
		if ($this->aTblblog === null && ($this->poll_blog !== null)) {
			$this->aTblblog = TblblogPeer::retrieveByPK($this->poll_blog, $con);
			
		}
		return $this->aTblblog;
	}

	
	public function clearTblblogpolldatas()
	{
		$this->collTblblogpolldatas = null; 	}

	
	public function initTblblogpolldatas()
	{
		$this->collTblblogpolldatas = array();
	}

	
	public function getTblblogpolldatas($criteria = null, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(TblblogpollPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTblblogpolldatas === null) {
			if ($this->isNew()) {
			   $this->collTblblogpolldatas = array();
			} else {

				$criteria->add(TblblogpolldataPeer::PD_POLL, $this->poll_id);

				TblblogpolldataPeer::addSelectColumns($criteria);
				$this->collTblblogpolldatas = TblblogpolldataPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(TblblogpolldataPeer::PD_POLL, $this->poll_id);

				TblblogpolldataPeer::addSelectColumns($criteria);
				if (!isset($this->lastTblblogpolldataCriteria) || !$this->lastTblblogpolldataCriteria->equals($criteria)) {
					$this->collTblblogpolldatas = TblblogpolldataPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastTblblogpolldataCriteria = $criteria;
		return $this->collTblblogpolldatas;
	}

	
	public function countTblblogpolldatas(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(TblblogpollPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collTblblogpolldatas === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(TblblogpolldataPeer::PD_POLL, $this->poll_id);

				$count = TblblogpolldataPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(TblblogpolldataPeer::PD_POLL, $this->poll_id);

				if (!isset($this->lastTblblogpolldataCriteria) || !$this->lastTblblogpolldataCriteria->equals($criteria)) {
					$count = TblblogpolldataPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collTblblogpolldatas);
				}
			} else {
				$count = count($this->collTblblogpolldatas);
			}
		}
		$this->lastTblblogpolldataCriteria = $criteria;
		return $count;
	}

	
	public function addTblblogpolldata(Tblblogpolldata $l)
	{
		if ($this->collTblblogpolldatas === null) {
			$this->initTblblogpolldatas();
		}
		if (!in_array($l, $this->collTblblogpolldatas, true)) { 			array_push($this->collTblblogpolldatas, $l);
			$l->setTblblogpoll($this);
		}
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			if ($this->collTblblogpolldatas) {
				foreach ((array) $this->collTblblogpolldatas as $o) {
					$o->clearAllReferences($deep);
				}
			}
		} 
		$this->collTblblogpolldatas = null;
			$this->aTblblog = null;
	}

} 