<?php


abstract class BaseAreas extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id_area;


	
	protected $descripcion;


	
	protected $sigla;


	
	protected $id_dependencia;


	
	protected $codigo;


	
	protected $id_nivel;

	//protected $estado;

	protected $collComisioness;


	protected $lastComisionesCriteria = null;
        
	protected $collProyectoss;

	
	protected $lastProyectosCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getIdArea()
	{

		return $this->id_area;
	}

	
	public function getDescripcion()
	{

		return $this->descripcion;
	}

	
	public function getSigla()
	{

		return $this->sigla;
	}

	
	public function getIdDependencia()
	{

		return $this->id_dependencia;
	}

	
	public function getCodigo()
	{

		return $this->codigo;
	}

	
	public function getIdNivel()
	{

		return $this->id_nivel;
	}

	/*
	public function getEstado()
	{

		return $this->estado;
	}*/

	
	public function setIdArea($v)
	{

		if ($this->id_area !== $v) {
			$this->id_area = $v;
			$this->modifiedColumns[] = AreasPeer::ID_AREA;
		}

	}

        public function addComisiones(Comisiones $l)
	{
		$this->collComisioness[] = $l;
		$l->setAreas($this);
	}

	
	public function setDescripcion($v)
	{

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

		if ($this->descripcion !== $v) {
			$this->descripcion = $v;
			$this->modifiedColumns[] = AreasPeer::DESCRIPCION;
		}

	} 
	
	public function setSigla($v)
	{

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

		if ($this->sigla !== $v) {
			$this->sigla = $v;
			$this->modifiedColumns[] = AreasPeer::SIGLA;
		}

	} 
	
	public function setIdDependencia($v)
	{

		if ($this->id_dependencia !== $v) {
			$this->id_dependencia = $v;
			$this->modifiedColumns[] = AreasPeer::ID_DEPENDENCIA;
		}

	} 
	
	public function setCodigo($v)
	{

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

		if ($this->codigo !== $v) {
			$this->codigo = $v;
			$this->modifiedColumns[] = AreasPeer::CODIGO;
		}

	} 
	
	public function setIdNivel($v)
	{

		if ($this->id_nivel !== $v) {
			$this->id_nivel = $v;
			$this->modifiedColumns[] = AreasPeer::ID_NIVEL;
		}

	} 
	/*
	public function setEstado($v)
	{

		if ($this->estado !== $v) {
			$this->estado = $v;
			$this->modifiedColumns[] = AreasPeer::ESTADO;
		}

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

			$this->id_area = $rs->getFloat($startcol + 0);

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

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

			$this->id_dependencia = $rs->getFloat($startcol + 3);

			$this->codigo = $rs->getString($startcol + 4);

			$this->id_nivel = $rs->getFloat($startcol + 5);

			//$this->estado = $rs->getFloat($startcol + 6);

			$this->resetModified();

			$this->setNew(false);

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

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

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


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

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getIdArea();
				break;
			case 1:
				return $this->getDescripcion();
				break;
			case 2:
				return $this->getSigla();
				break;
			case 3:
				return $this->getIdDependencia();
				break;
			case 4:
				return $this->getCodigo();
				break;
			case 5:
				return $this->getIdNivel();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = AreasPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getIdArea(),
			$keys[1] => $this->getDescripcion(),
			$keys[2] => $this->getSigla(),
			$keys[3] => $this->getIdDependencia(),
			$keys[4] => $this->getCodigo(),
			$keys[5] => $this->getIdNivel(),
			//$keys[6] => $this->getEstado(),
		);
		return $result;
	}

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

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setIdArea($value);
				break;
			case 1:
				$this->setDescripcion($value);
				break;
			case 2:
				$this->setSigla($value);
				break;
			case 3:
				$this->setIdDependencia($value);
				break;
			case 4:
				$this->setCodigo($value);
				break;
			case 5:
				$this->setIdNivel($value);
				break;
		} 	}

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

		if (array_key_exists($keys[0], $arr)) $this->setIdArea($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setDescripcion($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setSigla($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setIdDependencia($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setCodigo($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setIdNivel($arr[$keys[5]]);
		//if (array_key_exists($keys[6], $arr)) $this->setEstado($arr[$keys[6]]);
	}

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

		if ($this->isColumnModified(AreasPeer::ID_AREA)) $criteria->add(AreasPeer::ID_AREA, $this->id_area);
		if ($this->isColumnModified(AreasPeer::DESCRIPCION)) $criteria->add(AreasPeer::DESCRIPCION, $this->descripcion);
		if ($this->isColumnModified(AreasPeer::SIGLA)) $criteria->add(AreasPeer::SIGLA, $this->sigla);
		if ($this->isColumnModified(AreasPeer::ID_DEPENDENCIA)) $criteria->add(AreasPeer::ID_DEPENDENCIA, $this->id_dependencia);
		if ($this->isColumnModified(AreasPeer::CODIGO)) $criteria->add(AreasPeer::CODIGO, $this->codigo);
		if ($this->isColumnModified(AreasPeer::ID_NIVEL)) $criteria->add(AreasPeer::ID_NIVEL, $this->id_nivel);
		if ($this->isColumnModified(AreasPeer::ESTADO)) $criteria->add(AreasPeer::ESTADO, $this->estado);

		return $criteria;
	}

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

		$criteria->add(AreasPeer::ID_AREA, $this->id_area);

		return $criteria;
	}

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

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

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

		$copyObj->setDescripcion($this->descripcion);

		$copyObj->setSigla($this->sigla);

		$copyObj->setIdDependencia($this->id_dependencia);

		$copyObj->setCodigo($this->codigo);

		$copyObj->setIdNivel($this->id_nivel);

		//$copyObj->setEstado($this->estado);

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

			foreach($this->getProyectoss() as $relObj) {
				$copyObj->addProyectos($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

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

	
	public function initProyectoss()
	{
		if ($this->collProyectoss === null) {
			$this->collProyectoss = array();
		}
	}
        public function initComisioness()
	{
		if ($this->collComisioness === null) {
			$this->collComisioness = array();
		}
	}

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

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

				$criteria->add(ProyectosPeer::ID_AREA_LIDER, $this->getIdArea());

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

				$criteria->add(ProyectosPeer::ID_AREA_LIDER, $this->getIdArea());

				ProyectosPeer::addSelectColumns($criteria);
				if (!isset($this->lastProyectosCriteria) || !$this->lastProyectosCriteria->equals($criteria)) {
					$this->collProyectoss = ProyectosPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastProyectosCriteria = $criteria;
		return $this->collProyectoss;
	}

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

		$criteria->add(ProyectosPeer::ID_AREA_LIDER, $this->getIdArea());

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

	
	public function addProyectos(Proyectos $l)
	{
		$this->collProyectoss[] = $l;
		$l->setAreas($this);
	}

} 