<?php


abstract class BaseDepartamentos extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $coddepto;


	
	protected $desdepto;

	
	protected $collDestinoss;

	
	protected $lastDestinosCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getCoddepto()
	{

		return $this->coddepto;
	}

	
	public function getDesdepto()
	{

		return $this->desdepto;
	}

	
	public function setCoddepto($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->coddepto !== $v) {
			$this->coddepto = $v;
			$this->modifiedColumns[] = DepartamentosPeer::CODDEPTO;
		}

	} 
	
	public function setDesdepto($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->desdepto !== $v) {
			$this->desdepto = $v;
			$this->modifiedColumns[] = DepartamentosPeer::DESDEPTO;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->coddepto = $rs->getString($startcol + 0);

			$this->desdepto = $rs->getString($startcol + 1);

			$this->resetModified();

			$this->setNew(false);

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

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

		if ($con === null) {
			$con = Propel::getConnection(DepartamentosPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			DepartamentosPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

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

		if ($con === null) {
			$con = Propel::getConnection(DepartamentosPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

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


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

			if ($this->collDestinoss !== null) {
				foreach($this->collDestinoss 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 (($retval = DepartamentosPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collDestinoss !== null) {
					foreach($this->collDestinoss 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 = DepartamentosPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getCoddepto();
				break;
			case 1:
				return $this->getDesdepto();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = DepartamentosPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getCoddepto(),
			$keys[1] => $this->getDesdepto(),
		);
		return $result;
	}

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

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setCoddepto($value);
				break;
			case 1:
				$this->setDesdepto($value);
				break;
		} 	}

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

		if (array_key_exists($keys[0], $arr)) $this->setCoddepto($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setDesdepto($arr[$keys[1]]);
	}

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

		if ($this->isColumnModified(DepartamentosPeer::CODDEPTO)) $criteria->add(DepartamentosPeer::CODDEPTO, $this->coddepto);
		if ($this->isColumnModified(DepartamentosPeer::DESDEPTO)) $criteria->add(DepartamentosPeer::DESDEPTO, $this->desdepto);

		return $criteria;
	}

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

		$criteria->add(DepartamentosPeer::CODDEPTO, $this->coddepto);

		return $criteria;
	}

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

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

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

		$copyObj->setDesdepto($this->desdepto);


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

			foreach($this->getDestinoss() as $relObj) {
				$copyObj->addDestinos($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

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

	
	public function initDestinoss()
	{
		if ($this->collDestinoss === null) {
			$this->collDestinoss = array();
		}
	}

	
	public function getDestinoss($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

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

				$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

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

				$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

				DestinosPeer::addSelectColumns($criteria);
				if (!isset($this->lastDestinosCriteria) || !$this->lastDestinosCriteria->equals($criteria)) {
					$this->collDestinoss = DestinosPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastDestinosCriteria = $criteria;
		return $this->collDestinoss;
	}

	
	public function countDestinoss($criteria = null, $distinct = false, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

		return DestinosPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addDestinos(Destinos $l)
	{
		$this->collDestinoss[] = $l;
		$l->setDepartamentos($this);
	}


	
	public function getDestinossJoinMunicipios($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

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

				$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

				$this->collDestinoss = DestinosPeer::doSelectJoinMunicipios($criteria, $con);
			}
		} else {
									
			$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

			if (!isset($this->lastDestinosCriteria) || !$this->lastDestinosCriteria->equals($criteria)) {
				$this->collDestinoss = DestinosPeer::doSelectJoinMunicipios($criteria, $con);
			}
		}
		$this->lastDestinosCriteria = $criteria;

		return $this->collDestinoss;
	}


	
	public function getDestinossJoinComisiones($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

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

				$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

				$this->collDestinoss = DestinosPeer::doSelectJoinComisiones($criteria, $con);
			}
		} else {
									
			$criteria->add(DestinosPeer::CODDEPTO, $this->getCoddepto());

			if (!isset($this->lastDestinosCriteria) || !$this->lastDestinosCriteria->equals($criteria)) {
				$this->collDestinoss = DestinosPeer::doSelectJoinComisiones($criteria, $con);
			}
		}
		$this->lastDestinosCriteria = $criteria;

		return $this->collDestinoss;
	}

} 