<?php


abstract class BaseTbluserdocument extends BaseObject  implements Persistent {


	
	protected static $peer;

	
	protected $usdoc_user;

	
	protected $usdoc_document;

	
	protected $usdoc_date;

	
	protected $aTbldocument;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

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

	
	public function applyDefaultValues()
	{
	}

	
	public function getUsdocUser()
	{
		return $this->usdoc_user;
	}

	
	public function getUsdocDocument()
	{
		return $this->usdoc_document;
	}

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


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

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

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

		if ($this->usdoc_user !== $v) {
			$this->usdoc_user = $v;
			$this->modifiedColumns[] = TbluserdocumentPeer::USDOC_USER;
		}

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

		if ($this->usdoc_document !== $v) {
			$this->usdoc_document = $v;
			$this->modifiedColumns[] = TbluserdocumentPeer::USDOC_DOCUMENT;
		}

		if ($this->aTbldocument !== null && $this->aTbldocument->getDocId() !== $v) {
			$this->aTbldocument = null;
		}

		return $this;
	} 
	
	public function setUsdocDate($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->usdoc_date !== null || $dt !== null ) {
			
			$currNorm = ($this->usdoc_date !== null && $tmpDt = new DateTime($this->usdoc_date)) ? $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->usdoc_date = ($dt ? $dt->format('Y-m-d H:i:s') : null);
				$this->modifiedColumns[] = TbluserdocumentPeer::USDOC_DATE;
			}
		} 
		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->usdoc_user = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
			$this->usdoc_document = ($row[$startcol + 1] !== null) ? (int) $row[$startcol + 1] : null;
			$this->usdoc_date = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->resetModified();

			$this->setNew(false);

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

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

	
	public function ensureConsistency()
	{

		if ($this->aTbldocument !== null && $this->usdoc_document !== $this->aTbldocument->getDocId()) {
			$this->aTbldocument = 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(TbluserdocumentPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = TbluserdocumentPeer::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->aTbldocument = 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(TbluserdocumentPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			TbluserdocumentPeer::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(TbluserdocumentPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			$con->commit();
			TbluserdocumentPeer::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->aTbldocument !== null) {
				if ($this->aTbldocument->isModified() || $this->aTbldocument->isNew()) {
					$affectedRows += $this->aTbldocument->save($con);
				}
				$this->setTbldocument($this->aTbldocument);
			}


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

				$this->resetModified(); 			}

			$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->aTbldocument !== null) {
				if (!$this->aTbldocument->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aTbldocument->getValidationFailures());
				}
			}


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



			$this->alreadyInValidation = false;
		}

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

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

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getUsdocUser();
				break;
			case 1:
				return $this->getUsdocDocument();
				break;
			case 2:
				return $this->getUsdocDate();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = TbluserdocumentPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getUsdocUser(),
			$keys[1] => $this->getUsdocDocument(),
			$keys[2] => $this->getUsdocDate(),
		);
		return $result;
	}

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

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setUsdocUser($value);
				break;
			case 1:
				$this->setUsdocDocument($value);
				break;
			case 2:
				$this->setUsdocDate($value);
				break;
		} 	}

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

		if (array_key_exists($keys[0], $arr)) $this->setUsdocUser($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setUsdocDocument($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setUsdocDate($arr[$keys[2]]);
	}

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

		if ($this->isColumnModified(TbluserdocumentPeer::USDOC_USER)) $criteria->add(TbluserdocumentPeer::USDOC_USER, $this->usdoc_user);
		if ($this->isColumnModified(TbluserdocumentPeer::USDOC_DOCUMENT)) $criteria->add(TbluserdocumentPeer::USDOC_DOCUMENT, $this->usdoc_document);
		if ($this->isColumnModified(TbluserdocumentPeer::USDOC_DATE)) $criteria->add(TbluserdocumentPeer::USDOC_DATE, $this->usdoc_date);

		return $criteria;
	}

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

		$criteria->add(TbluserdocumentPeer::USDOC_USER, $this->usdoc_user);
		$criteria->add(TbluserdocumentPeer::USDOC_DOCUMENT, $this->usdoc_document);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		$pks = array();

		$pks[0] = $this->getUsdocUser();

		$pks[1] = $this->getUsdocDocument();

		return $pks;
	}

	
	public function setPrimaryKey($keys)
	{

		$this->setUsdocUser($keys[0]);

		$this->setUsdocDocument($keys[1]);

	}

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

		$copyObj->setUsdocUser($this->usdoc_user);

		$copyObj->setUsdocDocument($this->usdoc_document);

		$copyObj->setUsdocDate($this->usdoc_date);


		$copyObj->setNew(true);

	}

	
	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 TbluserdocumentPeer();
		}
		return self::$peer;
	}

	
	public function setTbldocument(Tbldocument $v = null)
	{
		if ($v === null) {
			$this->setUsdocDocument(NULL);
		} else {
			$this->setUsdocDocument($v->getDocId());
		}

		$this->aTbldocument = $v;

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

		return $this;
	}


	
	public function getTbldocument(PropelPDO $con = null)
	{
		if ($this->aTbldocument === null && ($this->usdoc_document !== null)) {
			$this->aTbldocument = TbldocumentPeer::retrieveByPK($this->usdoc_document, $con);
			
		}
		return $this->aTbldocument;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
		} 
			$this->aTbldocument = null;
	}

} 