PageRenderTime 269ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/core/libs/kumbia_active_record/kumbia_active_record.php

https://github.com/argordmel/Daily-Content-Manager
PHP | 2489 lines | 1608 code | 37 blank | 844 comment | 532 complexity | 9113c424374c2e16c5f50cf3d6c10bf3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * KumbiaPHP web & app Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://wiki.kumbiaphp.com/Licencia
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@kumbiaphp.com so we can send you a copy immediately.
  14. *
  15. * ActiveRecordBase Clase para el Mapeo Objeto Relacional
  16. *
  17. * @category Kumbia
  18. * @package Db
  19. * @subpackage ActiveRecord
  20. * @copyright Copyright (c) 2005-2009 Kumbia Team (http://www.kumbiaphp.com)
  21. * @license http://wiki.kumbiaphp.com/Licencia New BSD License
  22. */
  23. /**
  24. * @see Db
  25. */
  26. require CORE_PATH . 'libs/db/db.php';
  27. /**
  28. * ActiveRecordBase Clase para el Mapeo Objeto Relacional
  29. *
  30. * Active Record es un enfoque al problema de acceder a los datos de una
  31. * base de datos en forma orientada a objetos. Una fila en la
  32. * tabla de la base de datos (o vista) se envuelve en una clase,
  33. * de manera que se asocian filas &uacute;nicas de la base de datos
  34. * con objetos del lenguaje de programaci&oacute;n usado.
  35. * Cuando se crea uno de estos objetos, se a&ntilde;de una fila a
  36. * la tabla de la base de datos. Cuando se modifican los atributos del
  37. * objeto, se actualiza la fila de la base de datos.
  38. *
  39. * Propiedades Soportadas:
  40. * $db = Conexion al Motor de Base de datos
  41. * $database = Base de datos a la que se conecta, especificada en databases.ini
  42. * $source = Tabla que contiene la tabla que esta siendo mapeada
  43. * $fields = Listado de Campos de la tabla que han sido mapeados
  44. * $count = Conteo del ultimo Resultado de un Select
  45. * $primary_key = Listado de columnas que conforman la llave primaria
  46. * $non_primary = Listado de columnas que no son llave primaria
  47. * $not_null = Listado de campos que son not_null
  48. * $attributes_names = nombres de todos los campos que han sido mapeados
  49. * $debug = Indica si se deben mostrar los SQL enviados al RDBM en pantalla
  50. * $logger = Si es diferente de false crea un log utilizando la clase Logger
  51. * en library/kumbia/logger/logger.php, esta crea un archivo .txt en logs/ con todas las
  52. * operaciones realizadas en ActiveRecord, si $logger = "nombre", crea un
  53. * archivo con ese nombre
  54. *
  55. * Propiedades sin Soportar:
  56. * $dynamic_update : La idea es que en un futuro ActiveRecord solo
  57. * actualize los campos que han cambiado. (En Desarrollo)
  58. * $dynamic_insert : Indica si los valores del insert son solo aquellos
  59. * que sean no nulos. (En Desarrollo)
  60. * $select_before_update: Exige realizar una sentencia SELECT anterior
  61. * a la actualizacion UPDATE para comprobar que los datos no hayan sido
  62. * cambiados (En Desarrollo)
  63. * $subselect : Permitira crear una entidad ActiveRecord de solo lectura que
  64. * mapearia los resultados de un select directamente a un Objeto (En Desarrollo)
  65. *
  66. */
  67. class KumbiaActiveRecord
  68. {
  69. //Soportados
  70. /**
  71. * Resource de conexion a la base de datos
  72. *
  73. * @var DbBase
  74. */
  75. protected $db;
  76. /**
  77. * Base de datos a la que se conecta
  78. *
  79. * @var string
  80. */
  81. protected $database;
  82. /**
  83. * Schema donde esta la tabla
  84. *
  85. * @var string
  86. */
  87. protected $schema;
  88. /**
  89. * Tabla utilizada para realizar el mapeo
  90. *
  91. * @var string
  92. */
  93. protected $source;
  94. /**
  95. * Numero de resultados generados en la ultima consulta
  96. *
  97. * @var integer
  98. */
  99. protected $count;
  100. /**
  101. * Nombres de los atributos de la entidad
  102. *
  103. * @var array
  104. */
  105. protected $fields = array();
  106. /**
  107. * LLaves primarias de la entidad
  108. *
  109. * @var array
  110. */
  111. protected $primary_key = array();
  112. /**
  113. * Campos que no son llave primaria
  114. *
  115. * @var array
  116. */
  117. protected $non_primary = array();
  118. /**
  119. * Campos que no permiten nulos
  120. *
  121. * @var array
  122. */
  123. protected $not_null = array();
  124. /**
  125. * Campos que tienen valor por defecto
  126. *
  127. * @var array
  128. */
  129. protected $_with_default = array();
  130. /**
  131. * Nombres de atributos, es lo mismo que fields
  132. *
  133. * @var array
  134. */
  135. protected $alias = array();
  136. /**
  137. * Indica si la clase corresponde a un mapeo de una vista
  138. * en la base de datos
  139. *
  140. * @var boolean
  141. */
  142. protected $is_view = false;
  143. /**
  144. * Indica si el modelo esta en modo debug
  145. *
  146. * @var boolean
  147. */
  148. protected $debug = false;
  149. /**
  150. * Indica si se logearan los mensajes generados por la clase
  151. *
  152. * @var mixed
  153. */
  154. protected $logger = false;
  155. /**
  156. * Indica si los datos del modelo deben ser persistidos
  157. *
  158. * @var boolean
  159. */
  160. protected $persistent = false;
  161. /**
  162. * Validaciones
  163. *
  164. * inclusion_in: el campo pertenece a un conjunto de elementos
  165. * exclusion_of: el campo no pertenece a un conjunto de elementos
  166. * numericality_of: el campo debe ser numerico
  167. * format_of: el campo debe coincidir con la expresion regular compatible con perl
  168. * date_in: el campo debe ser una fecha valida
  169. * email_in: el campo debe ser un correo electronico
  170. * uniqueness_of: el campo debe ser unico
  171. *
  172. * @var array
  173. **/
  174. protected $_validates = array('inclusion_in' => array(), 'exclusion_of' => array(), 'numericality_of' => array(),
  175. 'format_of' => array(), 'date_in' => array(), 'email_in' => array(), 'uniqueness_of' => array());
  176. /**
  177. * Campos que terminan en _in
  178. *
  179. * @var array
  180. */
  181. protected $_in = array();
  182. /**
  183. * Campos que terminan en _at
  184. *
  185. * @var array
  186. */
  187. protected $_at = array();
  188. /**
  189. * Variable para crear una condicion basada en los
  190. * valores del where
  191. *
  192. * @var string
  193. */
  194. protected $_where_pk;
  195. /**
  196. * Indica si ya se han obtenido los metadatos del Modelo
  197. *
  198. * @var boolean
  199. */
  200. protected $_dumped = false;
  201. /**
  202. * Indica si hay bloqueo sobre los warnings cuando una propiedad
  203. * del modelo no esta definida-
  204. *
  205. * @var boolean
  206. */
  207. protected $_dump_lock = false;
  208. /**
  209. * Tipos de datos de los campos del modelo
  210. *
  211. * @var array
  212. */
  213. protected $_data_type = array();
  214. /**
  215. * Relaciones a las cuales tiene una cardinalidad 1-1
  216. *
  217. * @var array
  218. */
  219. protected $_has_one = array();
  220. /**
  221. * Relaciones a las cuales tiene una cardinalidad 1-n
  222. *
  223. * @var array
  224. */
  225. protected $_has_many = array();
  226. /**
  227. * Relaciones a las cuales tiene una cardinalidad 1-1
  228. *
  229. * @var array
  230. */
  231. protected $_belongs_to = array();
  232. /**
  233. * Relaciones a las cuales tiene una cardinalidad n-n (muchos a muchos) o 1-n inversa
  234. *
  235. * @var array
  236. */
  237. protected $_has_and_belongs_to_many = array();
  238. /**
  239. * Clases de las cuales es padre la clase actual
  240. *
  241. * @var array
  242. */
  243. protected $parent_of = array();
  244. /**
  245. * Persistance Models Meta-data
  246. */
  247. protected static $_models = array();
  248. /**
  249. * Persistance Models Meta-data
  250. */
  251. protected static $models = array();
  252. /**
  253. * Constructor del Modelo
  254. *
  255. * @param array $data
  256. */
  257. function __construct($data=null)
  258. {
  259. if (!$this->source) {
  260. $this->_model_name();
  261. }
  262. /**
  263. * Inicializa el modelo en caso de que exista initialize
  264. */
  265. if (method_exists($this, 'initialize')) {
  266. $this->initialize();
  267. }
  268. /**
  269. * Conecta a la bd
  270. **/
  271. $this->_connect();
  272. if ($data) {
  273. if (!is_array($data))
  274. $data = Util::getParams(func_get_args());
  275. $this->dump_result_self($data);
  276. }
  277. }
  278. /**
  279. * Obtiene el nombre de la relacion en el RDBM a partir del nombre de la clase
  280. *
  281. */
  282. protected function _model_name()
  283. {
  284. if (!$this->source) {
  285. $this->source = Util::uncamelize(get_class($this));
  286. }
  287. }
  288. /**
  289. * Establece publicamente el $source de la tabla
  290. *
  291. * @param string $source
  292. */
  293. public function set_source($source)
  294. {
  295. $this->source = $source;
  296. }
  297. /**
  298. * Devuelve el source actual
  299. *
  300. * @return string
  301. */
  302. public function get_source()
  303. {
  304. return $this->source;
  305. }
  306. /**
  307. * Establece la base datos a utilizar
  308. *
  309. * @param string $databse
  310. */
  311. public function set_database($database)
  312. {
  313. $this->database = $database;
  314. }
  315. /**
  316. * Devuelve la base de datos
  317. *
  318. * @return string
  319. */
  320. public function get_database()
  321. {
  322. if ($this->database) {
  323. return $this->database;
  324. } else {
  325. $core = Config::read('config');
  326. return $core['application']['database'];
  327. }
  328. }
  329. /**
  330. * Pregunta si el ActiveRecord ya ha consultado la informacion de metadatos
  331. * de la base de datos o del registro persistente
  332. *
  333. * @return boolean
  334. */
  335. public function is_dumped()
  336. {
  337. return $this->_dumped;
  338. }
  339. /**
  340. * Valida que los valores que sean leidos del objeto ActiveRecord esten definidos
  341. * previamente o sean atributos de la entidad
  342. *
  343. * @param string $property
  344. */
  345. function __get($property)
  346. {
  347. if (!$this->_dump_lock) {
  348. if (!isset($this->$property)) {
  349. if (array_key_exists($property, $this->_belongs_to)) {
  350. $relation = $this->_belongs_to[$property];
  351. return self::get($relation->model)->find_first($this->{$relation->fk});
  352. } elseif (array_key_exists($property, $this->_has_one)) {
  353. $relation = $this->_has_one[$property];
  354. if ($this->{$this->primary_key[0]}) {
  355. return self::get($relation->model)->find_first("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
  356. } else {
  357. return null;
  358. }
  359. } elseif (array_key_exists($property, $this->_has_many)) {
  360. $relation = $this->_has_many[$property];
  361. if ($this->{$this->primary_key[0]}) {
  362. return self::get($relation->model)->find("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
  363. } else {
  364. return array();
  365. }
  366. } elseif (array_key_exists($property, $this->_has_and_belongs_to_many)) {
  367. $relation = $this->_has_and_belongs_to_many[$property];
  368. $relation_model = self::get($relation->model);
  369. $relation_model->dump_model();
  370. $source = $this->source;
  371. $relation_source = $relation_model->source;
  372. /**
  373. * Cargo atraves de que tabla se efectuara la relacion
  374. *
  375. */
  376. if (!isset($relation->through)) {
  377. if ($source > $relation_source) {
  378. $relation->through = "{$this->source}_{$relation_source}";
  379. } else {
  380. $relation->through = "{$relation_source}_{$this->source}";
  381. }
  382. }
  383. if ($this->{$this->primary_key[0]}) {
  384. return self::get($relation->model)->find_all_by_sql("SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source
  385. WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]}) }
  386. AND {$relation->through}.{$relation->fk} = $relation_source.{$relation_model->primary_key[0]}
  387. AND {$relation->through}.{$relation->key} = $source.{$this->primary_key[0]}
  388. ORDER BY $relation_source.{$relation_model->primary_key[0]}");
  389. } else {
  390. return array();
  391. }
  392. } else {
  393. return null;
  394. }
  395. }
  396. }
  397. return $this->$property;
  398. }
  399. /**
  400. * Valida que los valores que sean asignados al objeto ActiveRecord esten definidos
  401. * o sean atributos de la entidad
  402. *
  403. * @param string $property
  404. * @param mixed $value
  405. */
  406. function __set($property, $value)
  407. {
  408. if (!$this->_dump_lock) {
  409. if (!isset($this->$property) && is_object($value) && is_subclass_of($value, 'ActiveRecordBase')) {
  410. if (array_key_exists($property, $this->_belongs_to)) {
  411. $relation = $this->_belongs_to[$property];
  412. $value->dump_model();
  413. $this->{$relation->fk} = $value->{$value->primary_key[0]};
  414. } elseif (array_key_exists($property, $this->_has_one)) {
  415. $relation = $this->_has_one[$property];
  416. $value->{$relation->fk} = $this->{$this->primary_key[0]};
  417. }
  418. } elseif ($property == "source") {
  419. $value = ActiveRecord::sql_item_sanizite($value);
  420. }
  421. }
  422. $this->$property = $value;
  423. }
  424. /**
  425. * Devuelve un valor o un listado dependiendo del tipo de Relaci&oacute;n
  426. *
  427. */
  428. public function __call($method, $args = array())
  429. {
  430. $has_relation = false;
  431. if (substr($method, 0, 8) == "find_by_") {
  432. $field = substr($method, 8);
  433. ActiveRecord::sql_item_sanizite($field);
  434. if (isset($args[0])) {
  435. $arg = array("conditions: $field = {$this->db->add_quotes($args[0]) }");
  436. unset($args[0]);
  437. } else {
  438. $arg = array();
  439. }
  440. return call_user_func_array(array($this, "find_first"), array_merge($arg, $args));
  441. }
  442. if (substr($method, 0, 9) == "count_by_") {
  443. $field = substr($method, 9);
  444. ActiveRecord::sql_item_sanizite($field);
  445. if (isset($args[0])) {
  446. $arg = array("conditions: $field = {$this->db->add_quotes($args[0]) }");
  447. unset($args[0]);
  448. } else {
  449. $arg = array();
  450. }
  451. return call_user_func_array(array($this, "count"), array_merge($arg, $args));
  452. }
  453. if (substr($method, 0, 12) == "find_all_by_") {
  454. $field = substr($method, 12);
  455. ActiveRecord::sql_item_sanizite($field);
  456. if (isset($args[0])) {
  457. $arg = array("conditions: $field = {$this->db->add_quotes($args[0]) }");
  458. unset($args[0]);
  459. } else {
  460. $arg = array();
  461. }
  462. return call_user_func_array(array($this, "find"), array_merge($arg, $args));
  463. }
  464. $model = preg_replace('/^get/', '', $method);
  465. $mmodel = Util::uncamelize($model);
  466. if (array_key_exists($mmodel, $this->_belongs_to)) {
  467. $has_relation = true;
  468. $relation = $this->_belongs_to[$mmodel];
  469. return self::get($relation->model)->find_first($this->{$relation->fk});
  470. }
  471. if (array_key_exists($mmodel, $this->_has_many)) {
  472. $has_relation = true;
  473. $relation = $this->_has_many[$mmodel];
  474. if ($this->{$this->primary_key[0]}) {
  475. return self::get($relation->model)->find("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
  476. } else {
  477. return array();
  478. }
  479. }
  480. if (array_key_exists($mmodel, $this->_has_one)) {
  481. $has_relation = true;
  482. $relation = $this->_has_one[$mmodel];
  483. if ($this->{$this->primary_key[0]}) {
  484. return self::get($relation->model)->find_first("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
  485. } else {
  486. return null;
  487. }
  488. }
  489. if (array_key_exists($mmodel, $this->_has_and_belongs_to_many)) {
  490. $has_relation = true;
  491. $relation = $this->_has_and_belongs_to_many[$mmodel];
  492. if ($this->{$this->primary_key[0]}) {
  493. $source = $this->source;
  494. $relation_model = self::get($relation->model);
  495. $relation_model->dump_model();
  496. $relation_source = $relation_model->source;
  497. /**
  498. * Cargo atraves de que tabla se efectuara la relacion
  499. *
  500. */
  501. if (!isset($relation->through)) {
  502. if ($source > $relation_source) {
  503. $relation->through = "{$this->source}_{$relation_source}";
  504. } else {
  505. $relation->through = "{$relation_source}_{$this->source}";
  506. }
  507. }
  508. return self::get($relation->model)->find_all_by_sql("SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source
  509. WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]}) }
  510. AND {$relation->through}.{$relation->fk} = $relation_source.{$relation_model->primary_key[0]}
  511. AND {$relation->through}.{$relation->key} = $source.{$this->primary_key[0]}
  512. ORDER BY $relation_source.{$relation_model->primary_key[0]}");
  513. } else {
  514. return array();
  515. }
  516. }
  517. try {
  518. if (method_exists($this, $method)) {
  519. call_user_func_array(array($this, $method), $args);
  520. } else {
  521. if ($has_relation) {
  522. throw new KumbiaException("No existe el modelo '$model' para relacionar con ActiveRecord::{$this->source}");
  523. } else {
  524. throw new KumbiaException("No existe el método '$method' en ActiveRecord::" . get_class($this));
  525. }
  526. }
  527. }
  528. catch(Exception $e) {
  529. $this->exceptions($e);
  530. }
  531. return $this->$method($args);
  532. }
  533. /**
  534. * Se conecta a la base de datos y descarga los meta-datos si es necesario
  535. *
  536. * @param boolean $new_connection
  537. */
  538. protected function _connect($new_connection = false)
  539. {
  540. if (!is_object($this->db) || $new_connection) {
  541. $this->db = Db::factory($this->database, $new_connection);
  542. }
  543. $this->db->debug = $this->debug;
  544. $this->db->logger = $this->logger;
  545. $this->dump();
  546. }
  547. /**
  548. * Cargar los metadatos de la tabla
  549. *
  550. */
  551. public function dump_model()
  552. {
  553. $this->_connect();
  554. }
  555. /**
  556. * Verifica si la tabla definida en $this->source existe
  557. * en la base de datos y la vuelca en dump_info
  558. *
  559. * @return boolean
  560. */
  561. protected function dump()
  562. {
  563. if ($this->_dumped) {
  564. return false;
  565. }
  566. $a = array();
  567. if ($this->source) {
  568. $this->source = str_replace(";", '', strtolower($this->source));
  569. } else {
  570. $this->_model_name();
  571. if (!$this->source) {
  572. return false;
  573. }
  574. }
  575. $table = $this->source;
  576. $schema = $this->schema;
  577. if (!count(self::get_meta_data($this->source))) {
  578. $this->_dumped = true;
  579. $this->_dump_info($table, $schema);
  580. if (!count($this->primary_key)) {
  581. if (!$this->is_view) {
  582. throw new KumbiaException("No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad");
  583. return false;
  584. }
  585. }
  586. } else {
  587. if (!$this->is_dumped()) {
  588. $this->_dumped = true;
  589. $this->_dump_info($table, $schema);
  590. }
  591. }
  592. return true;
  593. }
  594. /**
  595. * Vuelca la informaci&oacute;n de la tabla $table en la base de datos
  596. * para armar los atributos y meta-data del ActiveRecord
  597. *
  598. * @param string $table
  599. * @return boolean
  600. */
  601. protected function _dump_info($table, $schema = '')
  602. {
  603. $this->_dump_lock = true;
  604. if (!count(self::get_meta_data($table))) {
  605. $meta_data = $this->db->describe_table($table, $schema);
  606. if ($meta_data) {
  607. self::set_meta_data($table, $meta_data);
  608. }
  609. }
  610. foreach(self::get_meta_data($table) as $field) {
  611. $this->fields[] = $field['Field'];
  612. $aliasAux = $field['Field'];
  613. if ($field['Key'] == 'PRI') {
  614. $this->primary_key[] = $field['Field'];
  615. $this->alias[$field['Field']] = 'Código';
  616. } else $this->non_primary[] = $field['Field'];
  617. /**
  618. * Si se indica que no puede ser nulo, pero se indica un
  619. * valor por defecto, entonces no se incluye en la lista, ya que
  620. * al colocar un valor por defecto, el campo nunca sera nulo
  621. *
  622. */
  623. if ($field['Null'] == 'NO' && !(isset($field['Default']) && $field['Default'])) {
  624. $this->not_null[] = $field['Field'];
  625. }
  626. if (isset($field['Default']) && $field['Default']) {
  627. $this->_with_default[] = $field['Field'];
  628. }
  629. if ($field['Type']) {
  630. $this->_data_type[$field['Field']] = strtolower($field['Type']);
  631. }
  632. if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_at') {
  633. $this->_at[] = $field['Field'];
  634. $aliasAux = substr($field['Field'], 0, -3);
  635. }
  636. if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_in') {
  637. $this->_in[] = $field['Field'];
  638. $aliasAux = substr($field['Field'], 0, -3);
  639. }
  640. if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_id') {
  641. $aliasAux = substr($field['Field'], 0, -3);
  642. }
  643. //humanizando el alias
  644. $this->alias[$field['Field']] = ucwords(strtr($aliasAux,'_-',' '));
  645. }
  646. $this->_dump_lock = false;
  647. return true;
  648. }
  649. /**
  650. * Retorna un array de los campos (fields) de una tabla Humanizados
  651. *
  652. * @param $key
  653. * @return array
  654. */
  655. public function get_alias($key=null)
  656. {
  657. if($key && array_key_exists($key, $this->alias)){
  658. return $this->alias[$key];
  659. } else {
  660. throw new KumbiaException("No se pudo obtener el Alias, porque el key: \"$key\" no existe.");
  661. }
  662. return $this->alias;
  663. }
  664. /**
  665. * Asigna un nuevo valor al alias dado un key
  666. *
  667. * @param string $key
  668. * @param string $value
  669. */
  670. public function set_alias($key=null, $value=null)
  671. {
  672. if($key && array_key_exists($key, $this->alias)){
  673. $this->alias[$key] = $value;
  674. } else {
  675. throw new KumbiaException("No se pudo asignar el nuevo valor al Alias, porque el key: \"$key\" no existe.");
  676. }
  677. }
  678. /**
  679. * Commit a Transaction
  680. *
  681. * @return success
  682. */
  683. public function commit()
  684. {
  685. return $this->db->commit();
  686. }
  687. /**
  688. * Rollback a Transaction
  689. *
  690. * @return success
  691. */
  692. public function rollback()
  693. {
  694. return $this->db->rollback();
  695. }
  696. /**
  697. * Start a transaction in RDBM
  698. *
  699. * @return success
  700. */
  701. public function begin()
  702. {
  703. $this->_connect(true);
  704. return $this->db->begin();
  705. }
  706. /**
  707. * Find all records in this table using a SQL Statement
  708. *
  709. * @param string $sqlQuery
  710. * @return ActiveRecord Cursor
  711. */
  712. public function find_all_by_sql($sqlQuery)
  713. {
  714. $results = array();
  715. foreach($this->db->fetch_all($sqlQuery) as $result) {
  716. $results[] = $this->dump_result($result);
  717. }
  718. return $results;
  719. }
  720. /**
  721. * Find a record in this table using a SQL Statement
  722. *
  723. * @param string $sqlQuery
  724. * @return ActiveRecord Cursor
  725. */
  726. public function find_by_sql($sqlQuery)
  727. {
  728. $row = $this->db->fetch_one($sqlQuery);
  729. if ($row !== false) {
  730. $this->dump_result_self($row);
  731. return $this->dump_result($row);
  732. } else {
  733. return false;
  734. }
  735. }
  736. /**
  737. * Execute a SQL Statement directly
  738. *
  739. * @param string $sqlQuery
  740. * @return int affected
  741. */
  742. public function sql($sqlQuery)
  743. {
  744. return $this->db->query($sqlQuery);
  745. }
  746. /**
  747. * Return Fist Record
  748. *
  749. * @param mixed $what
  750. * @param boolean $debug
  751. *
  752. * Recibe los mismos parametros que find
  753. *
  754. * @return ActiveRecord Cursor
  755. */
  756. public function find_first($what = '')
  757. {
  758. $what = Util::getParams(func_get_args());
  759. $select = "SELECT ";
  760. if (isset($what['columns'])) {
  761. $select.= ActiveRecord::sql_sanizite($what['columns']);
  762. } elseif (isset($what['distinct'])) {
  763. $select.= 'DISTINCT ';
  764. $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields);
  765. } else {
  766. $select.= join(",", $this->fields);
  767. }
  768. if ($this->schema) {
  769. $select.= " FROM {$this->schema}.{$this->source}";
  770. } else {
  771. $select.= " FROM {$this->source}";
  772. }
  773. $what['limit'] = 1;
  774. $select.= $this->convert_params_to_sql($what);
  775. $resp = false;
  776. try {
  777. $result = $this->db->fetch_one($select);
  778. if ($result) {
  779. $this->dump_result_self($result);
  780. $resp = $this->dump_result($result);
  781. }
  782. }
  783. catch(Exception $e) {
  784. $this->exceptions($e);
  785. }
  786. return $resp;
  787. }
  788. /**
  789. * Find data on Relational Map table
  790. *
  791. * @param string $what
  792. * @return ActiveRecord Cursor
  793. *
  794. * columns: columnas a utilizar
  795. * conditions : condiciones de busqueda en WHERE
  796. * join: inclusion inner join o outer join
  797. * group : campo para grupo en GROUP BY
  798. * having : condicion para el grupo
  799. * order : campo para criterio de ordenamiento ORDER BY
  800. * distinct: campos para hacer select distinct
  801. */
  802. public function find($what = '')
  803. {
  804. $what = Util::getParams(func_get_args());
  805. $select = "SELECT ";
  806. if (isset($what['columns'])) {
  807. $select.= $what['columns'] ? ActiveRecord::sql_sanizite($what['columns']) : join(",", $this->fields);
  808. } elseif (isset($what['distinct'])) {
  809. $select.= 'DISTINCT ';
  810. $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields);
  811. } else {
  812. $select.= join(",", $this->fields);
  813. }
  814. if ($this->schema) {
  815. $select.= " FROM {$this->schema}.{$this->source}";
  816. } else {
  817. $select.= " FROM {$this->source}";
  818. }
  819. $select.= $this->convert_params_to_sql($what);
  820. $results = array();
  821. $all_results = $this->db->in_query($select);
  822. foreach($all_results AS $result) {
  823. $results[] = $this->dump_result($result);
  824. }
  825. $this->count = count($results, COUNT_NORMAL);
  826. if (isset($what[0]) && is_numeric($what[0])) {
  827. if (!isset($results[0])) {
  828. $this->count = 0;
  829. return false;
  830. } else {
  831. $this->dump_result_self($all_results[0]);
  832. $this->count = 1;
  833. return $results[0];
  834. }
  835. } else {
  836. $this->count = count($results, COUNT_NORMAL);
  837. return $results;
  838. }
  839. }
  840. /*
  841. * Arma una consulta SQL con el parametro $what, así:
  842. * $what = Util::getParams(func_get_args());
  843. * $select = "SELECT * FROM Clientes";
  844. * $select.= $this->convert_params_to_sql($what);
  845. *
  846. * @param string $what
  847. * @return string
  848. */
  849. public function convert_params_to_sql($what = '')
  850. {
  851. $select = '';
  852. if (is_array($what)) {
  853. if (!isset($what['conditions'])) {
  854. if (!isset($this->primary_key[0]) && (isset($this->id) || $this->is_view)) {
  855. $this->primary_key[0] = "id";
  856. }
  857. ActiveRecord::sql_item_sanizite($this->primary_key[0]);
  858. if (isset($what[0])) {
  859. if (is_numeric($what[0])) {
  860. $what['conditions'] = "{$this->primary_key[0]} = {$this->db->add_quotes($what[0]) }";
  861. } else {
  862. if ($what[0] == '') {
  863. $what['conditions'] = "{$this->primary_key[0]} = ''";
  864. } else {
  865. $what['conditions'] = $what[0];
  866. }
  867. }
  868. }
  869. }
  870. if (isset($what['join'])) {
  871. $select.= " {$what['join']}";
  872. }
  873. if (isset($what['conditions'])) {
  874. $select.= " WHERE {$what['conditions']}";
  875. }
  876. if (isset($what['group'])) {
  877. $select.= " GROUP BY {$what['group']}";
  878. }
  879. if (isset($what['having'])) {
  880. $select.= " HAVING {$what['having']}";
  881. }
  882. if (isset($what['order'])) {
  883. ActiveRecord::sql_sanizite($what['order']);
  884. $select.= " ORDER BY {$what['order']}";
  885. }
  886. $limit_args = array($select);
  887. if (isset($what['limit'])) {
  888. array_push($limit_args, "limit: $what[limit]");
  889. }
  890. if (isset($what['offset'])) {
  891. array_push($limit_args, "offset: $what[offset]");
  892. }
  893. if (count($limit_args) > 1) {
  894. $select = call_user_func_array(array($this, 'limit'), $limit_args);
  895. }
  896. } else {
  897. if (strlen($what)) {
  898. if (is_numeric($what)) {
  899. $select.= "WHERE {$this->primary_key[0]} = '$what'";
  900. } else {
  901. $select.= "WHERE $what";
  902. }
  903. }
  904. }
  905. return $select;
  906. }
  907. /*
  908. * Devuelve una clausula LIMIT adecuada al RDBMS empleado
  909. *
  910. * limit: maxima cantidad de elementos a mostrar
  911. * offset: desde que elemento se comienza a mostrar
  912. *
  913. * @param string $sql consulta select
  914. * @return String clausula LIMIT adecuada al RDBMS empleado
  915. */
  916. public function limit($sql)
  917. {
  918. $args = func_get_args();
  919. return call_user_func_array(array($this->db, 'limit'), $args);
  920. }
  921. /**
  922. * Ejecuta un SELECT DISTINCT
  923. * @param string $what
  924. * @return array
  925. *
  926. * Soporta parametros iguales a find
  927. *
  928. */
  929. public function distinct($what = '')
  930. {
  931. $what = Util::getParams(func_get_args());
  932. if ($this->schema) {
  933. $table = $this->schema . "." . $this->source;
  934. } else {
  935. $table = $this->source;
  936. }
  937. if (!isset($what['columns'])) {
  938. $what['columns'] = $what['0'];
  939. } else {
  940. if (!$what['columns']) {
  941. $what['columns'] = $what['0'];
  942. }
  943. }
  944. $what['columns'] = ActiveRecord::sql_sanizite($what['columns']);
  945. $select = "SELECT DISTINCT {$what['columns']} FROM $table ";
  946. /**
  947. * Se elimina el de indice cero ya que por defecto convert_params_to_sql lo considera como una condicion en WHERE
  948. */
  949. unset($what[0]);
  950. $select.= $this->convert_params_to_sql($what);
  951. $results = array();
  952. foreach($this->db->fetch_all($select) as $result) {
  953. $results[] = $result[0];
  954. }
  955. return $results;
  956. }
  957. /**
  958. * Ejecuta una consulta en el RDBM directamente
  959. *
  960. * @param string $sql
  961. * @return resource
  962. */
  963. static public function static_select_one($sql)
  964. {
  965. $db = Db::factory();
  966. if (substr(ltrim($sql), 0, 7) != "SELECT") {
  967. $sql = "SELECT " . $sql;
  968. }
  969. $num = $db->fetch_one($sql);
  970. return $num[0];
  971. }
  972. /**
  973. * Realiza un conteo de filas
  974. *
  975. * @param string $what
  976. * @return integer
  977. */
  978. public function count($what = '')
  979. {
  980. $what = Util::getParams(func_get_args());
  981. if ($this->schema) {
  982. $table = "{$this->schema}.{$this->source}";
  983. } else {
  984. $table = $this->source;
  985. }
  986. if (isset($what['distinct']) && $what['distinct']) {
  987. if (isset($what['group']) || isset($what['order'])) {
  988. $select = "SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table ";
  989. $select.= $this->convert_params_to_sql($what);
  990. $select.= ') AS t ';
  991. } else {
  992. $select = "SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table ";
  993. $select.= $this->convert_params_to_sql($what);
  994. }
  995. } else {
  996. $select = "SELECT COUNT(*) FROM $table ";
  997. $select.= $this->convert_params_to_sql($what);
  998. }
  999. $num = $this->db->fetch_one($select);
  1000. return $num[0];
  1001. }
  1002. /**
  1003. * Realiza un promedio sobre el campo $what
  1004. *
  1005. * @param string $what
  1006. * @return array
  1007. */
  1008. public function average($what = '')
  1009. {
  1010. $what = Util::getParams(func_get_args());
  1011. if (isset($what['column'])) {
  1012. if (!$what['column']) {
  1013. $what['column'] = $what[0];
  1014. }
  1015. } else {
  1016. $what['column'] = $what[0];
  1017. }
  1018. unset($what[0]);
  1019. ActiveRecord::sql_item_sanizite($what['column']);
  1020. if ($this->schema) {
  1021. $table = "{$this->schema}.{$this->source}";
  1022. } else {
  1023. $table = $this->source;
  1024. }
  1025. $select = "SELECT AVG({$what['column']}) FROM $table ";
  1026. $select.= $this->convert_params_to_sql($what);
  1027. $num = $this->db->fetch_one($select);
  1028. return $num[0];
  1029. }
  1030. public function sum($what = '')
  1031. {
  1032. $what = Util::getParams(func_get_args());
  1033. if (isset($what['column'])) {
  1034. if (!$what['column']) {
  1035. $what['column'] = $what[0];
  1036. }
  1037. } else {
  1038. $what['column'] = $what[0];
  1039. }
  1040. unset($what[0]);
  1041. ActiveRecord::sql_item_sanizite($what['column']);
  1042. if ($this->schema) {
  1043. $table = "{$this->schema}.{$this->source}";
  1044. } else {
  1045. $table = $this->source;
  1046. }
  1047. $select = "SELECT SUM({$what['column']}) FROM $table ";
  1048. $select.= $this->convert_params_to_sql($what);
  1049. $num = $this->db->fetch_one($select);
  1050. return $num[0];
  1051. }
  1052. /**
  1053. * Busca el valor maximo para el campo $what
  1054. *
  1055. * @param string $what
  1056. * @return mixed
  1057. */
  1058. public function maximum($what = '')
  1059. {
  1060. $what = Util::getParams(func_get_args());
  1061. if (isset($what['column'])) {
  1062. if (!$what['column']) {
  1063. $what['column'] = $what[0];
  1064. }
  1065. } else {
  1066. $what['column'] = $what[0];
  1067. }
  1068. unset($what[0]);
  1069. ActiveRecord::sql_item_sanizite($what['column']);
  1070. if ($this->schema) {
  1071. $table = "{$this->schema}.{$this->source}";
  1072. } else {
  1073. $table = $this->source;
  1074. }
  1075. $select = "SELECT MAX({$what['column']}) FROM $table ";
  1076. $select.= $this->convert_params_to_sql($what);
  1077. $num = $this->db->fetch_one($select);
  1078. return $num[0];
  1079. }
  1080. /**
  1081. * Busca el valor minimo para el campo $what
  1082. *
  1083. * @param string $what
  1084. * @return mixed
  1085. */
  1086. public function minimum($what = '')
  1087. {
  1088. $what = Util::getParams(func_get_args());
  1089. if (isset($what['column'])) {
  1090. if (!$what['column']) {
  1091. $what['column'] = $what[0];
  1092. }
  1093. } else {
  1094. $what['column'] = $what[0];
  1095. }
  1096. unset($what[0]);
  1097. ActiveRecord::sql_item_sanizite($what['column']);
  1098. if ($this->schema) {
  1099. $table = "{$this->schema}.{$this->source}";
  1100. } else {
  1101. $table = $this->source;
  1102. }
  1103. $select = "SELECT MIN({$what['column']}) FROM $table ";
  1104. $select.= $this->convert_params_to_sql($what);
  1105. $num = $this->db->fetch_one($select);
  1106. return $num[0];
  1107. }
  1108. /**
  1109. * Realiza un conteo directo mediante $sql
  1110. *
  1111. * @param string $sqlQuery
  1112. * @return mixed
  1113. */
  1114. public function count_by_sql($sqlQuery)
  1115. {
  1116. $num = $this->db->fetch_one($sqlQuery);
  1117. return $num[0];
  1118. }
  1119. /**
  1120. * Iguala los valores de un resultado de la base de datos
  1121. * en un nuevo objeto con sus correspondientes
  1122. * atributos de la clase
  1123. *
  1124. * @param array $result
  1125. * @return ActiveRecord
  1126. */
  1127. public function dump_result($result){
  1128. $obj = clone $this;
  1129. /**
  1130. * Consulta si la clase es padre de otra y crea el tipo de dato correcto
  1131. */
  1132. if (isset($result['type'])) {
  1133. if (in_array($result['type'], $this->parent_of)) {
  1134. if (class_exists($result['type'])) {
  1135. $obj = new $result['type'];
  1136. unset($result['type']);
  1137. }
  1138. }
  1139. }
  1140. $this->_dump_lock = true;
  1141. if (is_array($result)) {
  1142. foreach($result as $k => $r) {
  1143. if (!is_numeric($k)) {
  1144. $obj->$k = stripslashes($r);
  1145. }
  1146. }
  1147. }
  1148. $this->_dump_lock = false;
  1149. return $obj;
  1150. }
  1151. /**
  1152. * Iguala los valores de un resultado de la base de datos
  1153. * con sus correspondientes atributos de la clase
  1154. *
  1155. * @param array $result
  1156. * @return ActiveRecord
  1157. */
  1158. public function dump_result_self($result)
  1159. {
  1160. $this->_dump_lock = true;
  1161. if (is_array($result)) {
  1162. foreach($result as $k => $r) {
  1163. if (!is_numeric($k)) {
  1164. $this->$k = stripslashes($r);
  1165. }
  1166. }
  1167. }
  1168. $this->_dump_lock = false;
  1169. }
  1170. /**
  1171. * Crea un nuevo registro utilizando los datos del $_REQUEST
  1172. *
  1173. * @param string $form, equivalente a $_REQUEST[$form]
  1174. * @return boolean success
  1175. */
  1176. public function create_from_request($form = null)
  1177. {
  1178. if(!$form){
  1179. $form = $this->source;
  1180. }
  1181. $result = $this->create($_REQUEST[$form]);
  1182. if(!$result) {
  1183. Dispatcher::get_controller()->$form = $_REQUEST[$form];
  1184. }
  1185. return $result;
  1186. }
  1187. /**
  1188. * Saves a new Row using values from $_REQUEST
  1189. *
  1190. * @param string $form form name for request, equivalent to $_REQUEST[$form]
  1191. * @return boolean success
  1192. */
  1193. public function save_from_request($form = null)
  1194. {
  1195. if(!$form){
  1196. $form = $this->source;
  1197. }
  1198. $result = $this->save($_REQUEST[$form]);
  1199. if(!$result) {
  1200. Dispatcher::get_controller()->$form = $_REQUEST[$form];
  1201. }
  1202. return $result;
  1203. }
  1204. /**
  1205. * Updates a Row using values from $_REQUEST
  1206. *
  1207. * @param string $form form name for request, equivalent to $_REQUEST[$form]
  1208. * @return boolean success
  1209. */
  1210. public function update_from_request($form = null)
  1211. {
  1212. if(!$form){
  1213. $form = $this->source;
  1214. }
  1215. $result = $this->update($_REQUEST[$form]);
  1216. if(!$result) {
  1217. Dispatcher::get_controller()->$form = $_REQUEST[$form];
  1218. }
  1219. return $result;
  1220. }
  1221. /**
  1222. * Creates a new Row in map table
  1223. *
  1224. * @param mixed $values
  1225. * @return success boolean
  1226. */
  1227. public function create()
  1228. {
  1229. if (func_num_args() > 0) {
  1230. $params = Util::getParams(func_get_args());
  1231. $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params;
  1232. foreach($this->fields as $field) {
  1233. if (isset($values[$field])) {
  1234. $this->$field = $values[$field];
  1235. }
  1236. }
  1237. }
  1238. if ($this->primary_key[0] == 'id') {
  1239. $this->id = null;
  1240. }
  1241. return $this->save();
  1242. }
  1243. /**
  1244. * Consulta si un determinado registro existe o no
  1245. * en la entidad de la base de datos
  1246. *
  1247. * @return boolean
  1248. */
  1249. function exists($where_pk = '')
  1250. {
  1251. if ($this->schema) {
  1252. $table = "{$this->schema}.{$this->source}";
  1253. } else {
  1254. $table = $this->source;
  1255. }
  1256. if (!$where_pk) {
  1257. $where_pk = array();
  1258. foreach($this->primary_key as $key) {
  1259. if ($this->$key) {
  1260. $where_pk[] = " $key = '{$this->$key}'";
  1261. }
  1262. }
  1263. if (count($where_pk)) {
  1264. $this->_where_pk = join(" AND ", $where_pk);
  1265. } else {
  1266. return 0;
  1267. }
  1268. $query = "SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}";
  1269. } else {
  1270. if (is_numeric($where_pk)) {
  1271. $query = "SELECT(*) FROM $table WHERE id = '$where_pk'";
  1272. } else {
  1273. $query = "SELECT COUNT(*) FROM $table WHERE $where_pk";
  1274. }
  1275. }
  1276. $num = $this->db->fetch_one($query);
  1277. return $num[0];
  1278. }
  1279. /**
  1280. * Saves Information on the ActiveRecord Properties
  1281. * @param array $values array de valores a cargar
  1282. * @return boolean success
  1283. */
  1284. public function save($values=null)
  1285. {
  1286. if ($values) {
  1287. if(!is_array($values))
  1288. $values = Util::getParams(func_get_args());
  1289. foreach($this->fields as $field) {
  1290. if (isset($values[$field])) {
  1291. $this->$field = $values[$field];
  1292. }
  1293. }
  1294. }
  1295. $ex = $this->exists();
  1296. if ($this->schema) {
  1297. $table = $this->schema . "." . $this->source;
  1298. } else {
  1299. $table = $this->source;
  1300. }
  1301. #Run Validation Callbacks Before
  1302. if (method_exists($this, 'before_validation')) {
  1303. if ($this->before_validation() == 'cancel') {
  1304. return false;
  1305. }
  1306. } else {
  1307. if (isset($this->before_validation)) {
  1308. $method = $this->before_validation;
  1309. if ($this->$method() == 'cancel') {
  1310. return false;
  1311. }
  1312. }
  1313. }
  1314. if(!$ex){
  1315. if (method_exists($this, "before_validation_on_create")) {
  1316. if ($this->before_validation_on_create() == 'cancel') {
  1317. return false;
  1318. }
  1319. } else {
  1320. if (isset($this->before_validation_on_create)) {
  1321. $method = $this->before_validation_on_create;
  1322. if ($this->$method() == 'cancel') {
  1323. return false;
  1324. }
  1325. }
  1326. }
  1327. }
  1328. if($ex){
  1329. if (method_exists($this, "before_validation_on_update")) {
  1330. if ($this->before_validation_on_update() == 'cancel') {
  1331. return false;
  1332. }
  1333. } else {
  1334. if (isset($this->before_validation_on_update)) {
  1335. $method = $this->before_validation_on_update;
  1336. if($this->$method() == 'cancel') {
  1337. return false;
  1338. }
  1339. }
  1340. }
  1341. }
  1342. /**
  1343. * Validacion validates_presence
  1344. *
  1345. */
  1346. if(isset($this->_validates['presence_of'])) {
  1347. foreach($this->_validates['presence_of'] as $f => $opt) {
  1348. if (isset($this->$f) && (is_null($this->$f) || $this->$f == '')) {
  1349. if (!$ex && $f == 'id')
  1350. continue;
  1351. if (isset($opt['message'])) {
  1352. Flash::error($opt['message']);
  1353. return false;
  1354. } else {
  1355. $field = isset($opt['field']) ? $opt['field'] : $f;
  1356. Flash::error("Error: El campo $field no puede ser nulo");
  1357. return false;
  1358. }
  1359. }
  1360. }
  1361. }
  1362. /**
  1363. * Recordamos que aqui no aparecen los que tienen valores por defecto,
  1364. * pero sin embargo se debe estar pendiente de validar en las otras verificaciones
  1365. * los campos nulos, ya que en estas si el campo es nulo, realmente se refiere a un campo que
  1366. * debe tomar el valor por defecto
  1367. *
  1368. */
  1369. foreach ($this->not_null as $f) {
  1370. if(in_array($f, $this->_with_default)) {
  1371. continue;
  1372. }
  1373. if (!isset($this->$f) || is_null($this->$f) || $this->$f == '') {
  1374. if (!$ex && $f == 'id') {
  1375. continue;
  1376. }
  1377. if (!$ex && in_array($f, $this->_at)) {
  1378. continue;
  1379. }
  1380. if ($ex && in_array($f, $this->_in)) {
  1381. continue;
  1382. }
  1383. Flash::error("Error: El campo $f no puede ser nulo");
  1384. return false;
  1385. }
  1386. }
  1387. /**
  1388. * Validacion validates_length
  1389. *
  1390. */
  1391. if(isset($this->_validates['length_of'])) {
  1392. foreach($this->_validates['length_of'] as $f => $opt) {
  1393. if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
  1394. $field = isset($opt['field']) ? $opt['field'] : $f;
  1395. if (strlen($this->$f) < $opt['min']) {
  1396. if (isset($opt['too_short']))
  1397. Flash::error($opt['too_short']);
  1398. else
  1399. Flash::error("Error: El campo $field debe tener como mínimo $opt[min] caracteres");
  1400. return false;
  1401. }
  1402. if (strlen($this->$f) > $opt['max']) {
  1403. if (isset($opt['too_long']))
  1404. Flash::error($opt['too_long']);
  1405. else
  1406. Flash::error("Error: El campo $field debe tener como máximo $opt[max] caracteres");
  1407. return false;
  1408. }
  1409. }
  1410. }
  1411. }
  1412. /**
  1413. * Validacion validates_inclusion
  1414. *
  1415. */
  1416. foreach($this->_validates['inclusion_in'] as $f => $opt) {
  1417. if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
  1418. if (!in_array($this->$f, $opt['list'])) {
  1419. if (isset($opt['message'])) {
  1420. Flash::error($opt['message']);
  1421. } else {
  1422. $field = isset($opt['field']) ? $opt['field'] : $f;
  1423. Flash::error("$field debe tener un valor entre (" . join(",", $opt['list']) . ")");
  1424. }
  1425. return false;
  1426. }
  1427. }
  1428. }
  1429. /**
  1430. * Validacion validates_exclusion
  1431. *
  1432. */
  1433. foreach($this->_validates['exclusion_of'] as $f => $opt) {
  1434. if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
  1435. if (in_array($this->$f, $opt['list'])) {
  1436. if (isset($opt['message'])) {
  1437. Flash::error($opt['message']);
  1438. } else {
  1439. $field = isset($opt['field']) ? $opt['field'] : $f;
  1440. Flash::error("$field no debe tener un valor entre (" . join(",", $opt['list']) . ")");
  1441. }
  1442. return false;
  1443. }
  1444. }
  1445. }
  1446. /**
  1447. * Validacion validates_numericality
  1448. *
  1449. */
  1450. foreach($this->_validates['numericality_of'] as $f => $opt) {
  1451. if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
  1452. if (!is_numeric($this->$f)) {
  1453. if (isset($opt['message'])) {
  1454. Flash::error($opt['message']);
  1455. } else {
  1456. $field = isset($opt['field']) ? $opt['field'] : $f;
  1457. Flash::error("$field debe tener un valor numérico");
  1458. }
  1459. return false;
  1460. }
  1461. }
  1462. }
  1463. /**
  1464. * Validacion validates_format
  1465. *
  1466. */
  1467. foreach($this->_validates['format_of'] as $f => $opt) {
  1468. if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
  1469. if (!filter_var($this->$f, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>$opt['pattern'])))) {
  1470. if (isset($opt['message'])) {
  1471. Flash::error($opt['message']);
  1472. } else {
  1473. $field = isset($opt['field']) ? $opt['field'] : $f;
  1474. Flash::error("Formato erroneo para $field");
  1475. }
  1476. retu

Large files files are truncated, but you can click here to view the full file