PageRenderTime 33ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/estadisticas/estadisticas/application/models/catalogs/HelixTypesIdCatalog.php

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