<?php


abstract class BaseMunicipios extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $codmun;


	
	protected $municipio;


	
	protected $depto;

	
	protected $collDestinosComisionadoss;

	
	protected $lastDestinosComisionadosCriteria = null;

	
	protected $collDestinoss;

	
	protected $lastDestinosCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getCodmun()
	{

		return $this->codmun;
	}

	
	public function getMunicipio()
	{

		return $this->municipio;
	}

	
	public function getDepto()
	{

		return $this->depto;
	}

	
	public function setCodmun($v)
	{

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

		if ($this->codmun !== $v) {
			$this->codmun = $v;
			$this->modifiedColumns[] = MunicipiosPeer::CODMUN;
		}

	} 
	
	public function setMunicipio($v)
	{

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

		if ($this->municipio !== $v) {
			$this->municipio = $v;
			$this->modifiedColumns[] = MunicipiosPeer::MUNICIPIO;
		}

	} 
	
	public function setDepto($v)
	{

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

		if ($this->depto !== $v) {
			$this->depto = $v;
			$this->modifiedColumns[] = MunicipiosPeer::DEPTO;
		}

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

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

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

			$this->depto = $rs->getString($startcol + 2);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 3; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Municipios 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(MunicipiosPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			MunicipiosPeer::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(MunicipiosPeer::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 = MunicipiosPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setNew(false);
				} else {
					$affectedRows += MunicipiosPeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

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

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


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

				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 = MunicipiosPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getCodmun();
				break;
			case 1:
				return $this->getMunicipio();
				break;
			case 2:
				return $this->getDepto();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = MunicipiosPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getCodmun(),
			$keys[1] => $this->getMunicipio(),
			$keys[2] => $this->getDepto(),
		);
		return $result;
	}

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

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setCodmun($value);
				break;
			case 1:
				$this->setMunicipio($value);
				break;
			case 2:
				$this->setDepto($value);
				break;
		} 	}

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

		if (array_key_exists($keys[0], $arr)) $this->setCodmun($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setMunicipio($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setDepto($arr[$keys[2]]);
	}

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

		if ($this->isColumnModified(MunicipiosPeer::CODMUN)) $criteria->add(MunicipiosPeer::CODMUN, $this->codmun);
		if ($this->isColumnModified(MunicipiosPeer::MUNICIPIO)) $criteria->add(MunicipiosPeer::MUNICIPIO, $this->municipio);
		if ($this->isColumnModified(MunicipiosPeer::DEPTO)) $criteria->add(MunicipiosPeer::DEPTO, $this->depto);

		return $criteria;
	}

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

		$criteria->add(MunicipiosPeer::CODMUN, $this->codmun);

		return $criteria;
	}

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

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

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

		$copyObj->setMunicipio($this->municipio);

		$copyObj->setDepto($this->depto);


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

			foreach($this->getDestinosComisionadoss() as $relObj) {
				$copyObj->addDestinosComisionados($relObj->copy($deepCopy));
			}

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

		} 

		$copyObj->setNew(true);

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

	
	public function initDestinosComisionadoss()
	{
		if ($this->collDestinosComisionadoss === null) {
			$this->collDestinosComisionadoss = array();
		}
	}

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

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

				$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

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

				$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

				DestinosComisionadosPeer::addSelectColumns($criteria);
				if (!isset($this->lastDestinosComisionadosCriteria) || !$this->lastDestinosComisionadosCriteria->equals($criteria)) {
					$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastDestinosComisionadosCriteria = $criteria;
		return $this->collDestinosComisionadoss;
	}

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

		$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

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

	
	public function addDestinosComisionados(DestinosComisionados $l)
	{
		$this->collDestinosComisionadoss[] = $l;
		$l->setMunicipios($this);
	}


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

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

				$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

				$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelectJoinTerceros($criteria, $con);
			}
		} else {
									
			$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

			if (!isset($this->lastDestinosComisionadosCriteria) || !$this->lastDestinosComisionadosCriteria->equals($criteria)) {
				$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelectJoinTerceros($criteria, $con);
			}
		}
		$this->lastDestinosComisionadosCriteria = $criteria;

		return $this->collDestinosComisionadoss;
	}


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

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

				$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

				$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelectJoinTransportes($criteria, $con);
			}
		} else {
									
			$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

			if (!isset($this->lastDestinosComisionadosCriteria) || !$this->lastDestinosComisionadosCriteria->equals($criteria)) {
				$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelectJoinTransportes($criteria, $con);
			}
		}
		$this->lastDestinosComisionadosCriteria = $criteria;

		return $this->collDestinosComisionadoss;
	}


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

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

				$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

				$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelectJoinComisiones($criteria, $con);
			}
		} else {
									
			$criteria->add(DestinosComisionadosPeer::CODMUN, $this->getCodmun());

			if (!isset($this->lastDestinosComisionadosCriteria) || !$this->lastDestinosComisionadosCriteria->equals($criteria)) {
				$this->collDestinosComisionadoss = DestinosComisionadosPeer::doSelectJoinComisiones($criteria, $con);
			}
		}
		$this->lastDestinosComisionadosCriteria = $criteria;

		return $this->collDestinosComisionadoss;
	}

	
	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::CODMUN, $this->getCodmun());

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

				$criteria->add(DestinosPeer::CODMUN, $this->getCodmun());

				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::CODMUN, $this->getCodmun());

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

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


	
	public function getDestinossJoinDepartamentos($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::CODMUN, $this->getCodmun());

				$this->collDestinoss = DestinosPeer::doSelectJoinDepartamentos($criteria, $con);
			}
		} else {
									
			$criteria->add(DestinosPeer::CODMUN, $this->getCodmun());

			if (!isset($this->lastDestinosCriteria) || !$this->lastDestinosCriteria->equals($criteria)) {
				$this->collDestinoss = DestinosPeer::doSelectJoinDepartamentos($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::CODMUN, $this->getCodmun());

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

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

		return $this->collDestinoss;
	}

} 