PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/estadisticas/estadisticas/application/models/catalogs/PersonCatalog.php

https://bitbucket.org/xsngroup/estadisticas
PHP | 345 lines | 276 code | 11 blank | 58 comment | 5 complexity | e951b722f19deef7089cac164dd59910 MD5 | raw file
  1. <?php
  2. /**
  3. * Bender Modeler
  4. *
  5. * Our Simple Models
  6. *
  7. * @category lib
  8. * @package lib_models
  9. * @copyright Copyright (c) 2008-2010 Bender Modeler (http://www.ctrl-zetta.com/#code)
  10. * @author <zetta> <chentepixtol>, $LastChangedBy$
  11. * @version 1.0.0 SVN: $Id$
  12. */
  13. /**
  14. * Dependences
  15. */
  16. require_once "lib/db/Catalog.php";
  17. require_once "application/models/beans/Person.php";
  18. require_once "application/models/exceptions/PersonException.php";
  19. require_once "application/models/collections/PersonCollection.php";
  20. require_once "application/models/factories/PersonFactory.php";
  21. /**
  22. * Singleton PersonCatalog Class
  23. *
  24. * @category lib
  25. * @package lib_models
  26. * @subpackage lib_models_catalogs
  27. * @copyright Copyright (c) 2008-2010 Bender Modeler (http://www.ctrl-zetta.com/#code)
  28. * @copyright This File as been proudly generated by Bender (http://code.google.com/p/bender-modeler/). <chentepixtol> <zetta>
  29. * @author zetta & chentepixtol
  30. * @version 1.0.0 SVN: $Revision$
  31. */
  32. class PersonCatalog extends Catalog
  33. {
  34. /**
  35. * Singleton Instance
  36. * @var PersonCatalog
  37. */
  38. static protected $instance = null;
  39. /**
  40. * Método para obtener la instancia del catálogo
  41. * @return PersonCatalog
  42. */
  43. public static function getInstance()
  44. {
  45. if (!isset(self::$instance))
  46. {
  47. self::$instance = new self();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * Constructor de la clase PersonCatalog
  53. * @return PersonCatalog
  54. */
  55. protected function PersonCatalog()
  56. {
  57. parent::Catalog();
  58. }
  59. /**
  60. * Metodo para agregar un Person a la base de datos
  61. * @param Person $person Objeto Person
  62. */
  63. public function create($person)
  64. {
  65. if(!($person instanceof Person))
  66. throw new PersonException("passed parameter isn't a Person instance");
  67. try
  68. {
  69. $data = array(
  70. 'name' => $person->getName(),
  71. 'middle_name' => $person->getMiddleName(),
  72. 'last_name' => $person->getLastName(),
  73. 'date_birthdate' => $person->getDateBirthdate(),
  74. 'genre' => $person->getGenre(),
  75. 'marital_status' => $person->getMaritalStatus(),
  76. 'curp' => $person->getCurp(),
  77. );
  78. $data = array_filter($data, 'Catalog::notNull');
  79. $this->db->insert(Person::TABLENAME, $data);
  80. $person->setIdPerson($this->db->lastInsertId());
  81. }
  82. catch(Exception $e)
  83. {
  84. throw new PersonException("The Person can't be saved \n" . $e->getMessage());
  85. }
  86. }
  87. /**
  88. * Metodo para Obtener los datos de un objeto por su llave primaria
  89. * @param int $idPerson
  90. * @return Person|null
  91. */
  92. public function getById($idPerson)
  93. {
  94. try
  95. {
  96. $criteria = new Criteria();
  97. $criteria->add(Person::ID_PERSON, $idPerson, Criteria::EQUAL);
  98. $newPerson = $this->getByCriteria($criteria)->getOne();
  99. }
  100. catch(Exception $e)
  101. {
  102. throw new PersonException("Can't obtain the Person \n" . $e->getMessage());
  103. }
  104. return $newPerson;
  105. }
  106. /**
  107. * Metodo para Obtener una colección de objetos por varios ids
  108. * @param array $ids
  109. * @return PersonCollection
  110. */
  111. public function getByIds(array $ids)
  112. {
  113. if(null == $ids) return new PersonCollection();
  114. try
  115. {
  116. $criteria = new Criteria();
  117. $criteria->add(Person::ID_PERSON, $ids, Criteria::IN);
  118. $personCollection = $this->getByCriteria($criteria);
  119. }
  120. catch(Exception $e)
  121. {
  122. throw new PersonException("PersonCollection can't be populated\n" . $e->getMessage());
  123. }
  124. return $personCollection;
  125. }
  126. /**
  127. * Metodo para Obtener todos los ids en un arreglo
  128. * @return array
  129. */
  130. public function retrieveAllIds()
  131. {
  132. try
  133. {
  134. $result = $this->db->fetchCol('SELECT id_person FROM '.Person::TABLENAME);
  135. }
  136. catch(Exception $e)
  137. {
  138. throw new PersonException("Can't obtain the ids\n" . $e->getMessage());
  139. }
  140. return $result;
  141. }
  142. /**
  143. * Metodo para actualizar un Person
  144. * @param Person $person
  145. */
  146. public function update($person)
  147. {
  148. if(!($person instanceof Person))
  149. throw new PersonException("passed parameter isn't a Person instance");
  150. try
  151. {
  152. $where[] = "id_person = '{$person->getIdPerson()}'";
  153. $data = array(
  154. 'name' => $person->getName(),
  155. 'middle_name' => $person->getMiddleName(),
  156. 'last_name' => $person->getLastName(),
  157. 'date_birthdate' => $person->getDateBirthdate(),
  158. 'genre' => $person->getGenre(),
  159. 'marital_status' => $person->getMaritalStatus(),
  160. 'curp' => $person->getCurp(),
  161. );
  162. $data = array_filter($data, 'Catalog::notNull');
  163. $this->db->update(Person::TABLENAME, $data, $where);
  164. }
  165. catch(Exception $e)
  166. {
  167. throw new PersonException("The Person can't be updated \n" . $e->getMessage());
  168. }
  169. }
  170. /**
  171. * Metodo para guardar un person
  172. * @param Person $person
  173. */
  174. public function save($person)
  175. {
  176. if(!($person instanceof Person))
  177. throw new PersonException("passed parameter isn't a Person instance");
  178. if(null != $person->getIdPerson())
  179. $this->update($person);
  180. else
  181. $this->create($person);
  182. }
  183. /**
  184. * Metodo para eliminar un person
  185. * @param Person $person
  186. */
  187. public function delete($person)
  188. {
  189. if(!($person instanceof Person))
  190. throw new PersonException("passed parameter isn't a Person instance");
  191. $this->deleteById($person->getIdPerson());
  192. }
  193. /**
  194. * Metodo para eliminar un Person a partir de su Id
  195. * @param int $idPerson
  196. */
  197. public function deleteById($idPerson)
  198. {
  199. try
  200. {
  201. $where = array($this->db->quoteInto('id_person = ?', $idPerson));
  202. $this->db->delete(Person::TABLENAME, $where);
  203. }
  204. catch(Exception $e)
  205. {
  206. throw new PersonException("The Person can't be deleted\n" . $e->getMessage());
  207. }
  208. }
  209. /**
  210. * Metodo para eliminar varios Person a partir de su Id
  211. * @param array $ids
  212. */
  213. public function deleteByIds(array $ids)
  214. {
  215. try
  216. {
  217. $criteria = new Criteria();
  218. $criteria->add(Person::ID_PERSON, $ids, Criteria::IN);
  219. $this->db->delete(Person::TABLENAME, array($criteria->createSql()));
  220. }
  221. catch(Exception $e)
  222. {
  223. throw new PersonException("Can't delete that\n" . $e->getMessage());
  224. }
  225. }
  226. /**
  227. * Metodo para obtener todos los id de Person por criterio
  228. * @param Criteria $criteria
  229. * @return array Array con todos los id de Person que encajen en la busqueda
  230. */
  231. public function getIdsByCriteria(Criteria $criteria = null)
  232. {
  233. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  234. try
  235. {
  236. $sql = "SELECT id_person
  237. FROM ".Person::TABLENAME."
  238. WHERE " . $criteria->createSql();
  239. $ids = $this->db->fetchCol($sql);
  240. } catch(Exception $e)
  241. {
  242. throw new PersonException("Can't obtain Person's id\n" . $e->getMessage());
  243. }
  244. return $ids;
  245. }
  246. /**
  247. * Metodo para obtener un campo en particular de un Person dado un criterio
  248. * @param string $field
  249. * @param Criteria $criteria
  250. * @return array Array con el campo de los objetos Person que encajen en la busqueda
  251. */
  252. public function getCustomFieldByCriteria($field, Criteria $criteria = null)
  253. {
  254. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  255. try
  256. {
  257. $sql = "SELECT {$field}
  258. FROM ".Person::TABLENAME."
  259. WHERE " . $criteria->createSql();
  260. $result = $this->db->fetchCol($sql);
  261. } catch(Zend_Db_Exception $e)
  262. {
  263. throw new PersonException("No se pudieron obtener los ids de objetos {$Bean}\n" . $e->getMessage());
  264. }
  265. return $result;
  266. }
  267. /**
  268. * Metodo que regresa una coleccion de objetos Person
  269. * dependiendo del criterio establecido
  270. * @param Criteria $criteria
  271. * @return PersonCollection $personCollection
  272. */
  273. public function getByCriteria(Criteria $criteria = null)
  274. {
  275. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  276. $this->db->setFetchMode(Zend_Db::FETCH_ASSOC);
  277. try
  278. {
  279. $sql = "SELECT * FROM ".Person::TABLENAME."
  280. WHERE " . $criteria->createSql();
  281. $personCollection = new PersonCollection();
  282. foreach ($this->db->fetchAll($sql) as $result){
  283. $personCollection->append($this->getPersonInstance($result));
  284. }
  285. }
  286. catch(Zend_Db_Exception $e)
  287. {
  288. throw new PersonException("Cant obtain PersonCollection\n" . $e->getMessage());
  289. }
  290. return $personCollection;
  291. }
  292. /**
  293. * Metodo que cuenta Person
  294. * dependiendo del criterio establecido
  295. * @param Criteria $criteria
  296. * @param string $field
  297. * @return int $count
  298. */
  299. public function countByCriteria(Criteria $criteria = null, $field = 'id_person')
  300. {
  301. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  302. try
  303. {
  304. $sql = "SELECT COUNT( $field ) FROM ".Person::TABLENAME."
  305. WHERE " . $criteria->createSql();
  306. $count = $this->db->fetchOne($sql);
  307. }
  308. catch(Zend_Db_Exception $e)
  309. {
  310. throw new PersonException("Cant obtain the count \n" . $e->getMessage());
  311. }
  312. return $count;
  313. }
  314. /**
  315. * Método que construye un objeto Person y lo rellena con la información del rowset
  316. * @param array $result El arreglo que devolvió el objeto Zend_Db despues del fetch
  317. * @return Person
  318. */
  319. private function getPersonInstance($result)
  320. {
  321. return PersonFactory::createFromArray($result);
  322. }
  323. }