00001 <?php
00026 require_once CORE_PATH . 'extensions/db/active_record_base/exception.php';
00067 class ActiveRecordBase
00068 {
00069
00070
00076 public $db;
00082 protected $database;
00088 protected $schema;
00094 protected $source;
00100 public $count;
00106 public $fields = array();
00112 public $primary_key = array();
00118 public $non_primary = array();
00124 public $not_null = array();
00130 protected $_with_default = array();
00136 public $attributes_names = array();
00143 public $is_view = false;
00149 public $debug = false;
00155 public $logger = false;
00161 public $persistent = false;
00175 protected $_validates = array('inclusion_in' => array(), 'exclusion_of' => array(), 'numericality_of' => array(),
00176 'format_of' => array(), 'date_in' => array(), 'email_in' => array(), 'uniqueness_of' => array());
00182 protected $_in = array();
00188 protected $_at = array();
00195 protected $_where_pk;
00201 protected $_dumped = false;
00208 protected $_dump_lock = false;
00214 protected $_data_type = array();
00220 protected $_has_one = array();
00226 protected $_has_many = array();
00232 protected $_belongs_to = array();
00238 protected $_has_and_belongs_to_many = array();
00244 public $parent_of = array();
00248 protected static $_models = array();
00252 public static $models = array();
00258 function __construct($data=null)
00259 {
00260 if (!$this->source) {
00261 $this->_model_name();
00262 }
00263
00267 if (method_exists($this, 'initialize')) {
00268 $this->initialize();
00269 }
00270
00274 $this->_connect();
00275
00276 if ($data) {
00277 if (!is_array($data))
00278 $data = Util::getParams(func_get_args());
00279 $this->dump_result_self($data);
00280 }
00281 }
00286 protected function _model_name()
00287 {
00288 if (!$this->source) {
00289 $this->source = Util::uncamelize(get_class($this));
00290 }
00291 }
00297 public function set_source($source)
00298 {
00299 $this->source = $source;
00300 }
00306 public function get_source()
00307 {
00308 return $this->source;
00309 }
00315 public function set_database($database)
00316 {
00317 $this->database = $database;
00318 }
00324 public function get_database()
00325 {
00326 if ($this->database) {
00327 return $this->database;
00328 } else {
00329 $core = Config::read('config.ini');
00330 return $core['application']['database'];
00331 }
00332 }
00339 public function is_dumped()
00340 {
00341 return $this->_dumped;
00342 }
00349 function __get($property)
00350 {
00351 if (!$this->_dump_lock) {
00352 if (!isset($this->$property)) {
00353 if (array_key_exists($property, $this->_belongs_to)) {
00354 $relation = $this->_belongs_to[$property];
00355 return self::get($relation->model)->find_first($this->{$relation->fk});
00356 } elseif (array_key_exists($property, $this->_has_one)) {
00357 $relation = $this->_has_one[$property];
00358 if ($this->{$this->primary_key[0]}) {
00359 return self::get($relation->model)->find_first("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
00360 } else {
00361 return null;
00362 }
00363 } elseif (array_key_exists($property, $this->_has_many)) {
00364 $relation = $this->_has_many[$property];
00365 if ($this->{$this->primary_key[0]}) {
00366 return self::get($relation->model)->find("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
00367 } else {
00368 return array();
00369 }
00370 } elseif (array_key_exists($property, $this->_has_and_belongs_to_many)) {
00371 $relation = $this->_has_and_belongs_to_many[$property];
00372 $relation_model = self::get($relation->model);
00373 $relation_model->dump_model();
00374 $source = $this->source;
00375 $relation_source = $relation_model->source;
00380 if (!isset($relation->through)) {
00381 if ($source > $relation_source) {
00382 $relation->through = "{$this->source}_{$relation_source}";
00383 } else {
00384 $relation->through = "{$relation_source}_{$this->source}";
00385 }
00386 }
00387 if ($this->{$this->primary_key[0]}) {
00388 return self::get($relation->model)->find_all_by_sql("SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source
00389 WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]}) }
00390 AND {$relation->through}.{$relation->fk} = $relation_source.{$relation_model->primary_key[0]}
00391 AND {$relation->through}.{$relation->key} = $source.{$this->primary_key[0]}
00392 ORDER BY $relation_source.{$relation_model->primary_key[0]}");
00393 } else {
00394 return array();
00395 }
00396 } else {
00397 return null;
00398 }
00399 }
00400 }
00401 return $this->$property;
00402 }
00410 function __set($property, $value)
00411 {
00412 if (!$this->_dump_lock) {
00413 if (!isset($this->$property) && is_object($value) && is_subclass_of($value, 'ActiveRecordBase')) {
00414 if (array_key_exists($property, $this->_belongs_to)) {
00415 $relation = $this->_belongs_to[$property];
00416 $value->dump_model();
00417 $this->{$relation->fk} = $value->{$value->primary_key[0]};
00418 } elseif (array_key_exists($property, $this->_has_one)) {
00419 $relation = $this->_has_one[$property];
00420 $value->{$relation->fk} = $this->{$this->primary_key[0]};
00421 }
00422 } elseif ($property == "source") {
00423 $value = ActiveRecord::sql_item_sanizite($value);
00424 }
00425 }
00426 $this->$property = $value;
00427 }
00432 public function __call($method, $args = array())
00433 {
00434 $has_relation = false;
00435 if (substr($method, 0, 8) == "find_by_") {
00436 $field = substr($method, 8);
00437 ActiveRecord::sql_item_sanizite($field);
00438 if (isset($args[0])) {
00439 $arg = array("conditions: $field = {$this->db->add_quotes($args[0]) }");
00440 unset($args[0]);
00441 } else {
00442 $arg = array();
00443 }
00444 return call_user_func_array(array($this, "find_first"), array_merge($arg, $args));
00445 }
00446 if (substr($method, 0, 9) == "count_by_") {
00447 $field = substr($method, 9);
00448 ActiveRecord::sql_item_sanizite($field);
00449 if (isset($args[0])) {
00450 $arg = array("conditions: $field = {$this->db->add_quotes($args[0]) }");
00451 unset($args[0]);
00452 } else {
00453 $arg = array();
00454 }
00455 return call_user_func_array(array($this, "count"), array_merge($arg, $args));
00456 }
00457 if (substr($method, 0, 12) == "find_all_by_") {
00458 $field = substr($method, 12);
00459 ActiveRecord::sql_item_sanizite($field);
00460 if (isset($args[0])) {
00461 $arg = array("conditions: $field = {$this->db->add_quotes($args[0]) }");
00462 unset($args[0]);
00463 } else {
00464 $arg = array();
00465 }
00466 return call_user_func_array(array($this, "find"), array_merge($arg, $args));
00467 }
00468 $model = ereg_replace("^get", "", $method);
00469 $mmodel = Util::uncamelize($model);
00470 if (array_key_exists($mmodel, $this->_belongs_to)) {
00471 $has_relation = true;
00472 $relation = $this->_belongs_to[$mmodel];
00473 return self::get($relation->model)->find_first($this->{$relation->fk});
00474 }
00475 if (array_key_exists($mmodel, $this->_has_many)) {
00476 $has_relation = true;
00477 $relation = $this->_has_many[$mmodel];
00478 if ($this->{$this->primary_key[0]}) {
00479 return self::get($relation->model)->find("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
00480 } else {
00481 return array();
00482 }
00483 }
00484 if (array_key_exists($mmodel, $this->_has_one)) {
00485 $has_relation = true;
00486 $relation = $this->_has_one[$mmodel];
00487 if ($this->{$this->primary_key[0]}) {
00488 return self::get($relation->model)->find_first("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
00489 } else {
00490 return null;
00491 }
00492 }
00493 if (array_key_exists($mmodel, $this->_has_and_belongs_to_many)) {
00494 $has_relation = true;
00495 $relation = $this->_has_and_belongs_to_many[$mmodel];
00496 if ($this->{$this->primary_key[0]}) {
00497 $source = $this->source;
00498 $relation_model = self::get($relation->model);
00499 $relation_model->dump_model();
00500 $relation_source = $relation_model->source;
00505 if (!isset($relation->through)) {
00506 if ($source > $relation_source) {
00507 $relation->through = "{$this->source}_{$relation_source}";
00508 } else {
00509 $relation->through = "{$relation_source}_{$this->source}";
00510 }
00511 }
00512 return self::get($relation->model)->find_all_by_sql("SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source
00513 WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]}) }
00514 AND {$relation->through}.{$relation->fk} = $relation_source.{$relation_model->primary_key[0]}
00515 AND {$relation->through}.{$relation->key} = $source.{$this->primary_key[0]}
00516 ORDER BY $relation_source.{$relation_model->primary_key[0]}");
00517 } else {
00518 return array();
00519 }
00520 }
00521 try {
00522 if (method_exists($this, $method)) {
00523 call_user_func_array(array($this, $method), $args);
00524 } else {
00525 if ($has_relation) {
00526 throw new ActiveRecordException("No existe el modelo '$model' para relacionar con ActiveRecord::{$this->source}");
00527 } else {
00528 throw new ActiveRecordException("No existe el método '$method' en ActiveRecord::" . get_class($this));
00529 }
00530 }
00531 }
00532 catch(Exception $e) {
00533 $this->exceptions($e);
00534 }
00535 return $this->$method($args);
00536 }
00542 protected function _connect($new_connection = false)
00543 {
00544 if (!is_object($this->db) || $new_connection) {
00545 if ($this->database) {
00546 $this->db = DbBase::raw_connect($new_connection, $this->database);
00547 } else {
00548 $this->db = DbBase::raw_connect($new_connection);
00549 }
00550 }
00551 $this->db->debug = $this->debug;
00552 $this->db->logger = $this->logger;
00553 $this->dump();
00554 }
00559 public function dump_model()
00560 {
00561 $this->_connect();
00562 }
00569 protected function dump()
00570 {
00571 if ($this->_dumped) {
00572 return false;
00573 }
00574 $a = array();
00575 if ($this->source) {
00576 $this->source = str_replace(";", "", strtolower($this->source));
00577 } else {
00578 $this->_model_name();
00579 if (!$this->source) {
00580 return false;
00581 }
00582 }
00583 $table = $this->source;
00584 $schema = $this->schema;
00585 if (!count(self::get_meta_data($this->source))) {
00586 $this->_dumped = true;
00587 $this->_dump_info($table, $schema);
00588 if (!count($this->primary_key)) {
00589 if (!$this->is_view) {
00590 throw new ActiveRecordException("No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad");
00591 return false;
00592 }
00593 }
00594 } else {
00595 if (!$this->is_dumped()) {
00596 $this->_dumped = true;
00597 $this->_dump_info($table, $schema);
00598 }
00599 }
00600 return true;
00601 }
00609 protected function _dump_info($table, $schema = '')
00610 {
00611 $this->_dump_lock = true;
00612 if (!count(self::get_meta_data($table))) {
00613 $meta_data = $this->db->describe_table($table, $schema);
00614 if ($meta_data) {
00615 self::set_meta_data($table, $meta_data);
00616 }
00617 }
00618 foreach(self::get_meta_data($table) as $field) {
00619 $this->fields[] = $field['Field'];
00620 if ($field['Key'] == 'PRI') {
00621 $this->primary_key[] = $field['Field'];
00622 } else $this->non_primary[] = $field['Field'];
00629 if ($field['Null'] == 'NO' && !(isset($field['Default']) && $field['Default'])) {
00630 $this->not_null[] = $field['Field'];
00631 }
00632 if (isset($field['Default']) && $field['Default']) {
00633 $this->_with_default[] = $field['Field'];
00634 }
00635 if ($field['Type']) {
00636 $this->_data_type[$field['Field']] = strtolower($field['Type']);
00637 }
00638 if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_at') {
00639 $this->_at[] = $field['Field'];
00640 }
00641 if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_in') {
00642 $this->_in[] = $field['Field'];
00643 }
00644 }
00645 $this->attributes_names = $this->fields;
00646 $this->_dump_lock = false;
00647 return true;
00648 }
00654 public function commit()
00655 {
00656 return $this->db->commit();
00657 }
00663 public function rollback()
00664 {
00665 return $this->db->rollback();
00666 }
00672 public function begin()
00673 {
00674 $this->_connect(true);
00675 return $this->db->begin();
00676 }
00683 public function find_all_by_sql($sqlQuery)
00684 {
00685 $results = array();
00686 foreach($this->db->fetch_all($sqlQuery) as $result) {
00687 $results[] = $this->dump_result($result);
00688 }
00689 return $results;
00690 }
00697 public function find_by_sql($sqlQuery)
00698 {
00699 $row = $this->db->fetch_one($sqlQuery);
00700 if ($row !== false) {
00701 $this->dump_result_self($row);
00702 return $this->dump_result($row);
00703 } else {
00704 return false;
00705 }
00706 }
00713 public function sql($sqlQuery)
00714 {
00715 return $this->db->query($sqlQuery);
00716 }
00727 public function find_first($what = '')
00728 {
00729 $what = Util::getParams(func_get_args());
00730 $select = "SELECT ";
00731 if (isset($what['columns'])) {
00732 $select.= ActiveRecord::sql_sanizite($what['columns']);
00733 } elseif (isset($what['distinct'])) {
00734 $select.= 'DISTINCT ';
00735 $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields);
00736 } else {
00737 $select.= join(",", $this->fields);
00738 }
00739 if ($this->schema) {
00740 $select.= " FROM {$this->schema}.{$this->source}";
00741 } else {
00742 $select.= " FROM {$this->source}";
00743 }
00744 $what['limit'] = 1;
00745 $select.= $this->convert_params_to_sql($what);
00746 $resp = false;
00747 try {
00748 $result = $this->db->fetch_one($select);
00749 if ($result) {
00750 $this->dump_result_self($result);
00751 $resp = $this->dump_result($result);
00752 }
00753 }
00754 catch(Exception $e) {
00755 $this->exceptions($e);
00756 }
00757 return $resp;
00758 }
00773 public function find($what = '')
00774 {
00775 $what = Util::getParams(func_get_args());
00776 $select = "SELECT ";
00777 if (isset($what['columns'])) {
00778 $select.= $what['columns'] ? ActiveRecord::sql_sanizite($what['columns']) : join(",", $this->fields);
00779 } elseif (isset($what['distinct'])) {
00780 $select.= 'DISTINCT ';
00781 $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields);
00782 } else {
00783 $select.= join(",", $this->fields);
00784 }
00785 if ($this->schema) {
00786 $select.= " FROM {$this->schema}.{$this->source}";
00787 } else {
00788 $select.= " FROM {$this->source}";
00789 }
00790 $select.= $this->convert_params_to_sql($what);
00791 $results = array();
00792 $all_results = $this->db->in_query($select);
00793 foreach($all_results AS $result) {
00794 $results[] = $this->dump_result($result);
00795 }
00796 $this->count = count($results);
00797 if (isset($what[0]) && is_numeric($what[0])) {
00798 if (!isset($results[0])) {
00799 $this->count = 0;
00800 return false;
00801 } else {
00802 $this->dump_result_self($all_results[0]);
00803 $this->count = 1;
00804 return $results[0];
00805 }
00806 } else {
00807 $this->count = count($results);
00808 return $results;
00809 }
00810 }
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820 public function convert_params_to_sql($what = '')
00821 {
00822 $select = "";
00823 if (is_array($what)) {
00824 if (!isset($what['conditions'])) {
00825 if (!isset($this->primary_key[0]) && (isset($this->id) || $this->is_view)) {
00826 $this->primary_key[0] = "id";
00827 }
00828 ActiveRecord::sql_item_sanizite($this->primary_key[0]);
00829 if (isset($what[0])) {
00830 if (is_numeric($what[0])) {
00831 $what['conditions'] = "{$this->primary_key[0]} = {$this->db->add_quotes($what[0]) }";
00832 } else {
00833 if ($what[0] == '') {
00834 $what['conditions'] = "{$this->primary_key[0]} = ''";
00835 } else {
00836 $what['conditions'] = $what[0];
00837 }
00838 }
00839 }
00840 }
00841 if (isset($what['join']) && $what['join']) {
00842 $select.= " {$what['join']}";
00843 }
00844 if (isset($what['conditions']) && $what['conditions']) {
00845 $select.= " WHERE {$what['conditions']}";
00846 }
00847 if (isset($what['group']) && $what['group']) {
00848 $select.= " GROUP BY {$what['group']}";
00849 }
00850 if (isset($what['having']) && $what['having']) {
00851 $select.= " HAVING {$what['having']}";
00852 }
00853 if (isset($what['order']) && $what['order']) {
00854 ActiveRecord::sql_sanizite($what['order']);
00855 $select.= " ORDER BY {$what['order']}";
00856 }
00857 $limit_args = array($select);
00858 if (isset($what['limit'])) {
00859 array_push($limit_args, "limit: $what[limit]");
00860 }
00861 if (isset($what['offset'])) {
00862 array_push($limit_args, "offset: $what[offset]");
00863 }
00864 if (count($limit_args) > 1) {
00865 $select = call_user_func_array(array($this, 'limit'), $limit_args);
00866 }
00867 } else {
00868 if (strlen($what)) {
00869 if (is_numeric($what)) {
00870 $select.= "WHERE {$this->primary_key[0]} = '$what'";
00871 } else {
00872 $select.= "WHERE $what";
00873 }
00874 }
00875 }
00876 return $select;
00877 }
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887 public function limit($sql)
00888 {
00889 $args = func_get_args();
00890 return call_user_func_array(array($this->db, 'limit'), $args);
00891 }
00900 public function distinct($what = '')
00901 {
00902 $what = Util::getParams(func_get_args());
00903 if ($this->schema) {
00904 $table = $this->schema . "." . $this->source;
00905 } else {
00906 $table = $this->source;
00907 }
00908 if (!isset($what['columns'])) {
00909 $what['columns'] = $what['0'];
00910 } else {
00911 if (!$what['columns']) {
00912 $what['columns'] = $what['0'];
00913 }
00914 }
00915 $what['columns'] = ActiveRecord::sql_sanizite($what['columns']);
00916 $select = "SELECT DISTINCT {$what['columns']} FROM $table ";
00920 unset($what[0]);
00921 $select.= $this->convert_params_to_sql($what);
00922 $results = array();
00923 foreach($this->db->fetch_all($select) as $result) {
00924 $results[] = $result[0];
00925 }
00926 return $results;
00927 }
00934 public function select_one($sql)
00935 {
00936 if (substr(ltrim($sql), 0, 7) != "SELECT") {
00937 $sql = "SELECT " . $sql;
00938 }
00939 $num = $this->db->fetch_one($sql);
00940 return $num[0];
00941 }
00942 static public function static_select_one($sql)
00943 {
00944 $db = db::raw_connect();
00945 if (substr(ltrim($sql), 0, 7) != "SELECT") {
00946 $sql = "SELECT " . $sql;
00947 }
00948 $num = $db->fetch_one($sql);
00949 return $num[0];
00950 }
00957 public function count($what = '')
00958 {
00959 $what = Util::getParams(func_get_args());
00960 if ($this->schema) {
00961 $table = "{$this->schema}.{$this->source}";
00962 } else {
00963 $table = $this->source;
00964 }
00965 if (isset($what['distinct']) && $what['distinct']) {
00966 if (isset($what['group']) || isset($what['order'])) {
00967 $select = "SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table ";
00968 $select.= $this->convert_params_to_sql($what);
00969 $select.= ') AS t ';
00970 } else {
00971 $select = "SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table ";
00972 $select.= $this->convert_params_to_sql($what);
00973 }
00974 } else {
00975 $select = "SELECT COUNT(*) FROM $table ";
00976 $select.= $this->convert_params_to_sql($what);
00977 }
00978 $num = $this->db->fetch_one($select);
00979 return $num[0];
00980 }
00987 public function average($what = '')
00988 {
00989 $what = Util::getParams(func_get_args());
00990 if (isset($what['column'])) {
00991 if (!$what['column']) {
00992 $what['column'] = $what[0];
00993 }
00994 } else {
00995 $what['column'] = $what[0];
00996 }
00997 unset($what[0]);
00998 ActiveRecord::sql_item_sanizite($what['column']);
00999 if ($this->schema) {
01000 $table = "{$this->schema}.{$this->source}";
01001 } else {
01002 $table = $this->source;
01003 }
01004 $select = "SELECT AVG({$what['column']}) FROM $table ";
01005 $select.= $this->convert_params_to_sql($what);
01006 $num = $this->db->fetch_one($select);
01007 return $num[0];
01008 }
01009 public function sum($what = '')
01010 {
01011 $what = Util::getParams(func_get_args());
01012 if (isset($what['column'])) {
01013 if (!$what['column']) {
01014 $what['column'] = $what[0];
01015 }
01016 } else {
01017 $what['column'] = $what[0];
01018 }
01019 unset($what[0]);
01020 ActiveRecord::sql_item_sanizite($what['column']);
01021 if ($this->schema) {
01022 $table = "{$this->schema}.{$this->source}";
01023 } else {
01024 $table = $this->source;
01025 }
01026 $select = "SELECT SUM({$what['column']}) FROM $table ";
01027 $select.= $this->convert_params_to_sql($what);
01028 $num = $this->db->fetch_one($select);
01029 return $num[0];
01030 }
01037 public function maximum($what = '')
01038 {
01039 $what = Util::getParams(func_get_args());
01040 if (isset($what['column'])) {
01041 if (!$what['column']) {
01042 $what['column'] = $what[0];
01043 }
01044 } else {
01045 $what['column'] = $what[0];
01046 }
01047 unset($what[0]);
01048 ActiveRecord::sql_item_sanizite($what['column']);
01049 if ($this->schema) {
01050 $table = "{$this->schema}.{$this->source}";
01051 } else {
01052 $table = $this->source;
01053 }
01054 $select = "SELECT MAX({$what['column']}) FROM $table ";
01055 $select.= $this->convert_params_to_sql($what);
01056 $num = $this->db->fetch_one($select);
01057 return $num[0];
01058 }
01065 public function minimum($what = '')
01066 {
01067 $what = Util::getParams(func_get_args());
01068 if (isset($what['column'])) {
01069 if (!$what['column']) {
01070 $what['column'] = $what[0];
01071 }
01072 } else {
01073 $what['column'] = $what[0];
01074 }
01075 unset($what[0]);
01076 ActiveRecord::sql_item_sanizite($what['column']);
01077 if ($this->schema) {
01078 $table = "{$this->schema}.{$this->source}";
01079 } else {
01080 $table = $this->source;
01081 }
01082 $select = "SELECT MIN({$what['column']}) FROM $table ";
01083 $select.= $this->convert_params_to_sql($what);
01084 $num = $this->db->fetch_one($select);
01085 return $num[0];
01086 }
01093 public function count_by_sql($sqlQuery)
01094 {
01095 $num = $this->db->fetch_one($sqlQuery);
01096 return $num[0];
01097 }
01106 public function dump_result($result){
01107 $obj = clone $this;
01111 if (isset($result['type'])) {
01112 if (in_array($result['type'], $this->parent_of)) {
01113 if (class_exists($result['type'])) {
01114 $obj = new $result['type'];
01115 unset($result['type']);
01116 }
01117 }
01118 }
01119 $this->_dump_lock = true;
01120 if (is_array($result)) {
01121 foreach($result as $k => $r) {
01122 if (!is_numeric($k)) {
01123 $obj->$k = stripslashes($r);
01124 }
01125 }
01126 }
01127 $this->_dump_lock = false;
01128 return $obj;
01129 }
01137 public function dump_result_self($result)
01138 {
01139 $this->_dump_lock = true;
01140 if (is_array($result)) {
01141 foreach($result as $k => $r) {
01142 if (!is_numeric($k)) {
01143 $this->$k = stripslashes($r);
01144 }
01145 }
01146 }
01147 $this->_dump_lock = false;
01148 }
01155 public function create_from_request($form = '')
01156 {
01157 if ($form) {
01158 return $this->create($_REQUEST[$form]);
01159 } else {
01160 return $this->create($_REQUEST);
01161 }
01162 }
01169 public function save_from_request($form = '')
01170 {
01171 if ($form) {
01172 return $this->save($_REQUEST[$form]);
01173 } else {
01174 return $this->save($_REQUEST);
01175 }
01176 }
01183 public function update_from_request($form = '')
01184 {
01185 if ($form) {
01186 return $this->update($_REQUEST[$form]);
01187 } else {
01188 return $this->update($_REQUEST);
01189 }
01190 }
01197 public function create()
01198 {
01199 if (func_num_args() > 0) {
01200 $params = Util::getParams(func_get_args());
01201 $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params;
01202 foreach($this->fields as $field) {
01203 if (isset($values[$field])) {
01204 $this->$field = $values[$field];
01205 }
01206 }
01207 }
01208 if ($this->primary_key[0] == 'id') {
01209 $this->id = null;
01210 }
01211 return $this->save();
01212 }
01219 function exists($where_pk = '')
01220 {
01221 if ($this->schema) {
01222 $table = "{$this->schema}.{$this->source}";
01223 } else {
01224 $table = $this->source;
01225 }
01226 if (!$where_pk) {
01227 $where_pk = array();
01228 foreach($this->primary_key as $key) {
01229 if ($this->$key) {
01230 $where_pk[] = " $key = '{$this->$key}'";
01231 }
01232 }
01233 if (count($where_pk)) {
01234 $this->_where_pk = join(" AND ", $where_pk);
01235 } else {
01236 return 0;
01237 }
01238 $query = "SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}";
01239 } else {
01240 if (is_numeric($where_pk)) {
01241 $query = "SELECT(*) FROM $table WHERE id = '$where_pk'";
01242 } else {
01243 $query = "SELECT COUNT(*) FROM $table WHERE $where_pk";
01244 }
01245 }
01246 $num = $this->db->fetch_one($query);
01247 return $num[0];
01248 }
01254 public function save($values=null)
01255 {
01256 if ($values) {
01257 if(!is_array($values))
01258 $values = Util::getParams(func_get_args());
01259 foreach($this->fields as $field) {
01260 if (isset($values[$field])) {
01261 $this->$field = $values[$field];
01262 }
01263 }
01264 }
01265 $ex = $this->exists();
01266 if ($this->schema) {
01267 $table = $this->schema . "." . $this->source;
01268 } else {
01269 $table = $this->source;
01270 }
01271 #Run Validation Callbacks Before
01272 if (method_exists($this, 'before_validation')) {
01273 if ($this->before_validation() == 'cancel') {
01274 return false;
01275 }
01276 } else {
01277 if (isset($this->before_validation)) {
01278 $method = $this->before_validation;
01279 if ($this->$method() == 'cancel') {
01280 return false;
01281 }
01282 }
01283 }
01284 if(!$ex){
01285 if (method_exists($this, "before_validation_on_create")) {
01286 if ($this->before_validation_on_create() == 'cancel') {
01287 return false;
01288 }
01289 } else {
01290 if (isset($this->before_validation_on_create)) {
01291 $method = $this->before_validation_on_create;
01292 if ($this->$method() == 'cancel') {
01293 return false;
01294 }
01295 }
01296 }
01297 }
01298 if($ex){
01299 if (method_exists($this, "before_validation_on_update")) {
01300 if ($this->before_validation_on_update() == 'cancel') {
01301 return false;
01302 }
01303 } else {
01304 if (isset($this->before_validation_on_update)) {
01305 $method = $this->before_validation_on_update;
01306 if($this->$method() == 'cancel') {
01307 return false;
01308 }
01309 }
01310 }
01311 }
01312
01317 if(isset($this->_validates['presence_of'])) {
01318 foreach($this->_validates['presence_of'] as $f => $opt) {
01319 if (isset($this->$f) && (is_null($this->$f) || $this->$f == '')) {
01320 if (!$ex && $f == 'id')
01321 continue;
01322 if (isset($opt['message'])) {
01323 Flash::error($opt['message']);
01324 return false;
01325 } else {
01326 $field = isset($opt['field']) ? $opt['field'] : $f;
01327 Flash::error("Error: El campo $field no puede ser nulo");
01328 return false;
01329 }
01330 }
01331 }
01332 }
01333
01341 foreach ($this->not_null as $f) {
01342 if (!isset($this->$f) || is_null($this->$f) || $this->$f == '') {
01343 if (!$ex && $f == 'id') {
01344 continue;
01345 }
01346 if (!$ex && in_array($f, $this->_at)) {
01347 continue;
01348 }
01349 if ($ex && in_array($f, $this->_in)) {
01350 continue;
01351 }
01352 Flash::error("Error: El campo $f no puede ser nulo");
01353 return false;
01354 }
01355 }
01356
01361 if(isset($this->_validates['length_of'])) {
01362 foreach($this->_validates['length_of'] as $f => $opt) {
01363 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01364 $field = isset($opt['field']) ? $opt['field'] : $f;
01365
01366 if (strlen($this->$f) < $opt['min']) {
01367 if (isset($opt['too_short']))
01368 Flash::error($opt['too_short']);
01369 else
01370 Flash::error("Error: El campo $field debe tener como mínimo $opt[min] caracteres");
01371 return false;
01372 }
01373
01374 if (strlen($this->$f) > $opt['max']) {
01375 if (isset($opt['too_long']))
01376 Flash::error($opt['too_long']);
01377 else
01378 Flash::error("Error: El campo $field debe tener como máximo $opt[max] caracteres");
01379 return false;
01380 }
01381 }
01382 }
01383 }
01384
01389 foreach($this->_validates['inclusion_in'] as $f => $opt) {
01390 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01391 if (!in_array($this->$f, $opt['list'])) {
01392 if (isset($opt['message'])) {
01393 Flash::error($opt['message']);
01394 } else {
01395 $field = isset($opt['field']) ? $opt['field'] : $f;
01396 Flash::error("$field debe tener un valor entre (" . join(",", $opt['list']) . ")");
01397 }
01398 return false;
01399 }
01400 }
01401 }
01402
01407 foreach($this->_validates['exclusion_of'] as $f => $opt) {
01408 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01409 if (in_array($this->$f, $opt['list'])) {
01410 if (isset($opt['message'])) {
01411 Flash::error($opt['message']);
01412 } else {
01413 $field = isset($opt['field']) ? $opt['field'] : $f;
01414 Flash::error("$field no debe tener un valor entre (" . join(",", $opt['list']) . ")");
01415 }
01416 return false;
01417 }
01418 }
01419 }
01420
01425 foreach($this->_validates['numericality_of'] as $f => $opt) {
01426 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01427 if (!is_numeric($this->$f)) {
01428 if (isset($opt['message'])) {
01429 Flash::error($opt['message']);
01430 } else {
01431 $field = isset($opt['field']) ? $opt['field'] : $f;
01432 Flash::error("$field debe tener un valor numérico");
01433 }
01434 return false;
01435 }
01436 }
01437 }
01438
01443 foreach($this->_validates['format_of'] as $f => $opt) {
01444 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01445 if (!filter_var($this->$f, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>$opt['pattern'])))) {
01446 if (isset($opt['message'])) {
01447 Flash::error($opt['message']);
01448 } else {
01449 $field = isset($opt['field']) ? $opt['field'] : $f;
01450 Flash::error("Formato erroneo para $field");
01451 }
01452 return false;
01453 }
01454 }
01455 }
01456
01461 foreach($this->_validates['date_in'] as $f => $opt) {
01462 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01463 if (!filter_var($this->$f, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^\d{4}[-\/](0[1-9]|1[012])[-\/](0[1-9]|[12][0-9]|3[01])$/")))) {
01464 if (isset($opt['message'])) {
01465 Flash::error($opt['message']);
01466 } else {
01467 $field = isset($opt['field']) ? $opt['field'] : $f;
01468 Flash::error("Formato de fecha erroneo para $field");
01469 }
01470 return false;
01471 }
01472 }
01473 }
01474
01479 foreach($this->_validates['email_in'] as $f=>$opt) {
01480 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01481 if (!filter_var($this->$f, FILTER_VALIDATE_EMAIL)) {
01482 if (isset($opt['message'])) {
01483 Flash::error($opt['message']);
01484 } else {
01485 $field = isset($opt['field']) ? $opt['field'] : $f;
01486 Flash::error("Formato de e-mail erroneo en el campo $field");
01487 }
01488 return false;
01489 }
01490 }
01491 }
01492
01497 foreach($this->_validates['uniqueness_of'] as $f => $opt) {
01498 if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') {
01499 $result = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $f = {$this->db->add_quotes($this->$f)}");
01500 if ($result[0]) {
01501 if (isset($opt['message'])) {
01502 Flash::error($opt['message']);
01503 } else {
01504 $field = isset($opt['field']) ? $opt['field'] : $f;
01505 Flash::error("El valor '{$this->$f}' ya existe para el campo $field");
01506 }
01507 return false;
01508 }
01509 }
01510 }
01511
01512 #Run Validation Callbacks After
01513 if(!$ex){
01514 if (method_exists($this, "after_validation_on_create")) {
01515 if ($this->after_validation_on_create() == 'cancel') {
01516 return false;
01517 }
01518 } else {
01519 if (isset($this->after_validation_on_create)) {
01520 $method = $this->after_validation_on_create;
01521 if ($this->$method() == 'cancel') {
01522 return false;
01523 }
01524 }
01525 }
01526 }
01527 if($ex){
01528 if (method_exists($this, "after_validation_on_update")) {
01529 if ($this->after_validation_on_update() == 'cancel') {
01530 return false;
01531 }
01532 } else {
01533 if (isset($this->after_validation_on_update)) {
01534 $method = $this->after_validation_on_update;
01535 if ($this->$method() == 'cancel') return false;
01536 }
01537 }
01538 }
01539
01540 if (method_exists($this, 'after_validation')) {
01541 if ($this->after_validation() == 'cancel') {
01542 return false;
01543 }
01544 } else {
01545 if (isset($this->after_validation)) {
01546 $method = $this->after_validation;
01547 if ($this->$method() == 'cancel') {
01548 return false;
01549 }
01550 }
01551 }
01552 # Run Before Callbacks
01553 if (method_exists($this, "before_save")) {
01554 if ($this->before_save() == 'cancel') {
01555 return false;
01556 }
01557 } else {
01558 if (isset($this->before_save)) {
01559 $method = $this->before_save;
01560 if ($this->$method() == 'cancel') {
01561 return false;
01562 }
01563 }
01564 }
01565 if($ex){
01566 if (method_exists($this, "before_update")) {
01567 if ($this->before_update() == 'cancel') {
01568 return false;
01569 }
01570 } else {
01571 if (isset($this->before_update)) {
01572 $method = $this->before_update;
01573 if ($this->$method() == 'cancel') {
01574 return false;
01575 }
01576 }
01577 }
01578 }
01579 if(!$ex){
01580 if (method_exists($this, "before_create")) {
01581 if ($this->before_create() == 'cancel') {
01582 return false;
01583 }
01584 } else {
01585 if (isset($this->before_create)) {
01586 $method = $this->before_create;
01587 if ($this->$method() == 'cancel') {
01588 return false;
01589 }
01590 }
01591 }
01592 }
01593 $environment = Config::read('databases.ini');
01594 $config = $environment[$this->get_database()];
01595 if ($ex) {
01596 $fields = array();
01597 $values = array();
01598 foreach($this->non_primary as $np) {
01599 $np = ActiveRecord::sql_item_sanizite($np);
01600 if (in_array($np, $this->_in)) {
01601 if ($config['type'] == 'oracle') {
01602 $this->$np = date("Y-m-d");
01603 } else {
01604 $this->$np = date("Y-m-d G:i:s");
01605 }
01606 }
01607 if (isset($this->$np)) {
01608 $fields[] = $np;
01609 if (is_null($this->$np) || $this->$np == '') {
01610 $values[] = "NULL";
01611 } elseif (substr($this->$np, 0, 1) == "%") {
01612 $values[] = str_replace("%", "", $this->$np);
01613 } else {
01617 if ($this->_data_type[$np] == 'date' && $config['type'] == 'oracle') {
01618 $values[] = "TO_DATE(" . $this->db->add_quotes($this->$np) . ", 'YYYY-MM-DD')";
01619 } else {
01620 $values[] = $this->db->add_quotes($this->$np);
01621 }
01622 }
01623 }
01624 }
01625 $val = $this->db->update($table, $fields, $values, $this->_where_pk);
01626 } else {
01627 $fields = array();
01628 $values = array();
01629 foreach($this->fields as $field) {
01630 if ($field != 'id' && !$this->id) {
01631 if (in_array($field, $this->_at)) {
01632 if ($config['type'] == 'oracle') {
01633 $this->$field = date("Y-m-d");
01634 } else {
01635 $this->$field = date("Y-m-d G:i:s");
01636 }
01637 }
01638 if (in_array($field, $this->_in)) {
01639 unset($this->$field);
01640 }
01641 $use_default = in_array($field, $this->_with_default) && isset($this->$field) && (is_null($this->$field) || $this->$field == '');
01642 if($this->_data_type[$field] == 'datetime' && $config['type'] == 'mysql'){
01643 $this->$field = date("Y-m-d G:i:s",strtotime($this->$field));
01644 }
01645 if (isset($this->$field) && !$use_default) {
01646 $fields[] = ActiveRecord::sql_sanizite($field);
01647 if (substr($this->$field, 0, 1) == "%") {
01648 $values[] = str_replace("%", "", $this->$field);
01649 } else {
01650 if ($this->is_a_numeric_type($field) || $this->$field == null) {
01651 $values[] = addslashes($this->$field !== '' && $this->$field !== null ? $this->$field : "NULL");
01652 } else {
01653 if ($this->_data_type[$field] == 'date' && $config['type'] == 'oracle') {
01657 $values[] = "TO_DATE(" . $this->db->add_quotes($this->$field) . ", 'YYYY-MM-DD')";
01658 } else {
01659 if (!is_null($this->$field) && $this->$field != '') {
01660 $values[] = $this->db->add_quotes($this->$field);
01661 } else {
01662 $values[] = "NULL";
01663 }
01664 }
01665 }
01666 }
01667 }
01668 } else {
01672 if ($config['type'] == 'oracle') {
01673 if (!$this->id) {
01674 $fields[] = "id";
01675 $values[] = $this->source . "_id_seq.NEXTVAL";
01676 }
01677 }
01678 if ($config['type'] == 'informix') {
01679 if (!$this->id) {
01680 $fields[] = "id";
01681 $values[] = 0;
01682 }
01683 }
01684 }
01685 }
01686 $val = $this->db->insert($table, $values, $fields);
01687 }
01688 if (!isset($config['pdo']) && $config['type'] == 'oracle') {
01689 $this->commit();
01690 }
01691 if (!$ex) {
01692 $this->db->logger = true;
01693 $m = $this->db->last_insert_id($table, $this->primary_key[0]);
01694 $this->find_first($m);
01695 }
01696 if ($val) {
01697 if($ex){
01698 if (method_exists($this, "after_update")) {
01699 if ($this->after_update() == 'cancel') {
01700 return false;
01701 }
01702 } else {
01703 if (isset($this->after_update)) {
01704 $method = $this->after_update;
01705 if ($this->$method() == 'cancel') {
01706 return false;
01707 }
01708 }
01709 }
01710 }
01711 if(!$ex){
01712 if (method_exists($this, "after_create")) {
01713 if ($this->after_create() == 'cancel') {
01714 return false;
01715 }
01716 } else {
01717 if (isset($this->after_create)) {
01718 $method = $this->after_create;
01719 if ($this->$method() == 'cancel') {
01720 return false;
01721 }
01722 }
01723 }
01724 }
01725 if (method_exists($this, "after_save")) {
01726 if ($this->after_save() == 'cancel') {
01727 return false;
01728 }
01729 } else {
01730 if (isset($this->after_save)) {
01731 $method = $this->after_save;
01732 if ($this->$method() == 'cancel') {
01733 return false;
01734 }
01735 }
01736 }
01737 return $val;
01738 } else {
01739 return false;
01740 }
01741 }
01749 function find_all_by($field, $value)
01750 {
01751 ActiveRecord::sql_item_sanizite($field);
01752 return $this->find("conditions: $field = {$this->db->add_quotes($value) }");
01753 }
01760 function update()
01761 {
01762 if (func_num_args() > 0) {
01763 $params = Util::getParams(func_get_args());
01764 $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params;
01765 foreach($this->fields as $field) {
01766 if (isset($values[$field])) {
01767 $this->$field = $values[$field];
01768 }
01769 }
01770 }
01771 if ($this->exists()) {
01772 return $this->save();
01773 } else {
01774 Flash::error('No se puede actualizar porque el registro no existe');
01775 return false;
01776 }
01777 }
01783 public function delete($what = '')
01784 {
01785 if (func_num_args() > 1) {
01786 $what = Util::getParams(func_get_args());
01787 }
01788 if ($this->schema) {
01789 $table = $this->schema . "." . $this->source;
01790 } else {
01791 $table = $this->source;
01792 }
01793 $conditions = "";
01794 if (is_array($what)) {
01795 if ($what["conditions"]) {
01796 $conditions = $what["conditions"];
01797 }
01798 } else {
01799 if (is_numeric($what)) {
01800 ActiveRecord::sql_sanizite($this->primary_key[0]);
01801 $conditions = "{$this->primary_key[0]} = '$what'";
01802 } else {
01803 if ($what) {
01804 $conditions = $what;
01805 } else {
01806 ActiveRecord::sql_sanizite($this->primary_key[0]);
01807 $conditions = "{$this->primary_key[0]} = '{$this->{$this->primary_key[0]}}'";
01808 }
01809 }
01810 }
01811 if (method_exists($this, "before_delete")) {
01812 if ($this->id) {
01813 $this->find($this->id);
01814 }
01815 if ($this->before_delete() == 'cancel') {
01816 return false;
01817 }
01818 } else {
01819 if (isset($this->before_delete)) {
01820 if ($this->id) {
01821 $this->find($this->id);
01822 }
01823 $method = $this->before_delete;
01824 if ($this->$method() == 'cancel') {
01825 return false;
01826 }
01827 }
01828 }
01829 $val = $this->db->delete($table, $conditions);
01830 if ($val) {
01831 if (method_exists($this, "after_delete")) {
01832 if ($this->after_delete() == 'cancel') {
01833 return false;
01834 }
01835 } else {
01836 if (isset($this->after_delete)) {
01837 $method = $this->after_delete;
01838 if ($this->$method() == 'cancel') {
01839 return false;
01840 }
01841 }
01842 }
01843 }
01844 return $val;
01845 }
01853 public function update_all($values) {
01854 $params = array();
01855 if ($this->schema) {
01856 $table = $this->schema . "." . $this->source;
01857 } else {
01858 $table = $this->source;
01859 }
01860 if (func_num_args() > 1) {
01861 $params = Util::getParams(func_get_args());
01862 }
01863 if (!isset($params['conditions']) || !$params['conditions']) {
01864 if (isset($params[1])) {
01865 $params['conditions'] = $params[1];
01866 } else {
01867 $params['conditions'] = "";
01868 }
01869 }
01870 if ($params['conditions']) {
01871 $params['conditions'] = " WHERE " . $params['conditions'];
01872 }
01873 $sql = "UPDATE $table SET $values {$params['conditions']}";
01874 $limit_args = array($sql);
01875 if (isset($params['limit'])) {
01876 array_push($limit_args, "limit: $params[limit]");
01877 }
01878 if (isset($params['offset'])) {
01879 array_push($limit_args, "offset: $params[offset]");
01880 }
01881 if (count($limit_args) > 1) {
01882 $sql = call_user_func_array(array($this, 'limit'), $limit_args);
01883 }
01884 $environment = Config::read('databases.ini');
01885 $config = $environment->{$this->get_database() };
01886 if (!isset($config->pdo) || !$config->pdo) {
01887 if ($config->type == "informix") {
01888 $this->db->set_return_rows(false);
01889 }
01890 }
01891 return $this->db->query($sql);
01892 }
01899 public function delete_all($conditions = '') {
01900 $limit = "";
01901 if ($this->schema) {
01902 $table = $this->schema . "." . $this->source;
01903 } else {
01904 $table = $this->source;
01905 }
01906 if (func_num_args() > 1) {
01907 $params = Util::getParams(func_get_args());
01908 $limit_args = array($select);
01909 if (isset($params['limit'])) {
01910 array_push($limit_args, "limit: $params[limit]");
01911 }
01912 if (isset($params['offset'])) {
01913 array_push($limit_args, "offset: $params[offset]");
01914 }
01915 if (count($limit_args) > 1) {
01916 $select = call_user_func_array(array($this, 'limit'), $limit_args);
01917 }
01918 }
01919 return $this->db->delete($table, $conditions);
01920 }
01931 public function inspect() {
01932 $inspect = array();
01933 foreach($this->fields as $field) {
01934 if (!is_array($field)) {
01935 $inspect[] = "$field: {$this->$field}";
01936 }
01937 }
01938 return join(", ", $inspect);
01939 }
01954 protected function validates_presence_of($field, $params=array())
01955 {
01956 if (is_string($params))
01957 $params = Util::getParams(func_get_args());
01958
01959 $this->_validates['presence_of'][$field] = $params;
01960 }
01974 protected function validates_length_of($field, $max, $min=0, $params=array())
01975 {
01976 if (is_string($params))
01977 $params = Util::getParams(func_get_args());
01978
01979 $this->_validates['length_of'][$field] = $params;
01980 $this->_validates['length_of'][$field]['min'] = $min;
01981 $this->_validates['length_of'][$field]['max'] = $max;
01982 }
01993 protected function validates_inclusion_in($field, $list, $params=array())
01994 {
01995 if (is_string($params))
01996 $params = Util::getParams(func_get_args());
01997
01998 $this->_validates['inclusion_in'][$field] = $params;
01999 $this->_validates['inclusion_in'][$field]['list'] = $list;
02000 }
02011 protected function validates_exclusion_of($field, $list, $params=array())
02012 {
02013 if (is_string($params))
02014 $params = Util::getParams(func_get_args());
02015
02016 $this->_validates['exclusion_of'][$field] = $params;
02017 $this->_validates['exclusion_of'][$field]['list'] = $list;
02018 }
02029 protected function validates_format_of($field, $pattern, $params=array())
02030 {
02031 if (is_string($params))
02032 $params = Util::getParams(func_get_args());
02033
02034 $this->_validates['format_of'][$field] = $params;
02035 $this->_validates['format_of'][$field]['pattern'] = $pattern;
02036 }
02046 protected function validates_numericality_of($field, $params=array())
02047 {
02048 if (is_string($params))
02049 $params = Util::getParams(func_get_args());
02050
02051 $this->_validates['numericality_of'][$field] = $params;
02052 }
02062 protected function validates_email_in($field, $params=array())
02063 {
02064 if (is_string($params))
02065 $params = Util::getParams(func_get_args());
02066
02067 $this->_validates['email_in'][$field] = $params;
02068 }
02078 protected function validates_uniqueness_of($field, $params=array())
02079 {
02080 if (is_string($params))
02081 $params = Util::getParams(func_get_args());
02082
02083 $this->_validates['uniqueness_of'][$field] = $params;
02084 }
02094 protected function validates_date_in($field, $params=array())
02095 {
02096 if (is_string($params))
02097 $params = Util::getParams(func_get_args());
02098
02099 $this->_validates['date_in'][$field] = $params;
02100 }
02107 public function is_a_numeric_type($field)
02108 {
02109 if (strpos(" " . $this->_data_type[$field], "int") || strpos(" " . $this->_data_type[$field], "decimal") || strpos(" " . $this->_data_type[$field], "number")) {
02110 return true;
02111 } else {
02112 return false;
02113 }
02114 }
02121 static function get_meta_data($table)
02122 {
02123 if (isset(self::$models[$table])) {
02124 return self::$models[$table];
02125 } elseif($metadata = Cache::get($table, 'kumbia.models')) {
02126 self::$models[$table] = unserialize($metadata);
02127 return $metadata;
02128 }
02129 return array();
02130 }
02137 static function set_meta_data($table, $meta_data)
02138 {
02139 Cache::save(serialize($meta_data), Config::get('config.application.metadata_lifetime'), $table, 'kumbia.models');
02140 self::$models[$table] = $meta_data;
02141 return true;
02142 }
02143
02144
02145
02154 protected function has_one($relation)
02155 {
02156 $params = Util::getParams(func_get_args());
02157 for ($i = 0;isset($params[$i]);$i++) {
02158 $relation = Util::uncamelize($params[$i]);
02159 if (!array_key_exists($relation, $this->_has_one)) {
02160 $this->_has_one[$relation] = new stdClass();
02161 $this->_has_one[$relation]->model = isset($params['model']) ? $params['model'] : $relation;
02162 $this->_has_one[$relation]->fk = isset($params['fk']) ? $params['fk'] : Util::uncamelize(get_class($this)) . '_id';
02163 }
02164 }
02165 }
02174 protected function belongs_to($relation)
02175 {
02176 $params = Util::getParams(func_get_args());
02177 for ($i = 0;isset($params[$i]);$i++) {
02178 $relation = Util::uncamelize($params[$i]);
02179 if (!array_key_exists($relation, $this->_belongs_to)) {
02180 $this->_belongs_to[$relation] = new stdClass();
02181 $this->_belongs_to[$relation]->model = isset($params['model']) ? $params['model'] : $relation;
02182 $this->_belongs_to[$relation]->fk = isset($params['fk']) ? $params['fk'] : "{$relation}_id";
02183 }
02184 }
02185 }
02194 protected function has_many($relation)
02195 {
02196 $params = Util::getParams(func_get_args());
02197 for ($i = 0;isset($params[$i]);$i++) {
02198 $relation = Util::uncamelize($params[$i]);
02199 if (!array_key_exists($relation, $this->_has_many)) {
02200 $this->_has_many[$relation] = new stdClass();
02201 $this->_has_many[$relation]->model = isset($params['model']) ? $params['model'] : $relation;
02202 $this->_has_many[$relation]->fk = isset($params['fk']) ? $params['fk'] : Util::uncamelize(get_class($this)) . '_id';
02203 }
02204 }
02205 }
02216 protected function has_and_belongs_to_many($relation)
02217 {
02218 $params = Util::getParams(func_get_args());
02219 for ($i = 0;isset($params[$i]);$i++) {
02220 $relation = Util::uncamelize($params[$i]);
02221 if (!array_key_exists($relation, $this->_has_and_belongs_to_many)) {
02222 $this->_has_and_belongs_to_many[$relation] = new stdClass();
02223 $this->_has_and_belongs_to_many[$relation]->model = isset($params['model']) ? $params['model'] : $relation;
02224 $this->_has_and_belongs_to_many[$relation]->fk = isset($params['fk']) ? $params['fk'] : "{$relation}_id";
02225 $this->_has_and_belongs_to_many[$relation]->key = isset($params['key']) ? $params['key'] : Util::uncamelize(get_class($this)) . '_id';
02226 if (isset($params['through'])) {
02227 $this->_has_and_belongs_to_many[$relation]->through = $params['through'];
02228 }
02229 }
02230 }
02231 }
02240 public function parent_of($parent)
02241 {
02242 $parents = func_get_args();
02243 foreach($parents as $parent) {
02244 if (!in_array($parent, $this->parent_of)) {
02245 $this->parent_of[] = $parent;
02246 }
02247 }
02248 }
02255 public static function sql_item_sanizite($sql_item)
02256 {
02257 $sql_item = trim($sql_item);
02258 if ($sql_item !== '' && $sql_item !== null) {
02259 $sql_item = ereg_replace("[ ]+", "", $sql_item);
02260 if (!ereg("^[a-zA-Z0-9_\.]+$", $sql_item)) {
02261 throw new ActiveRecordException("Se esta tratando de ejecutar una operacion maliciosa!");
02262 }
02263 }
02264 return $sql_item;
02265 }
02272 public static function sql_sanizite($sql_item)
02273 {
02274 $sql_item = trim($sql_item);
02275 if ($sql_item !== '' && $sql_item !== null) {
02276 $sql_item = ereg_replace("[ ]+", "", $sql_item);
02277 if (!ereg("^[a-zA-Z_0-9\,\(\)\.\*]+$", $sql_item)) {
02278 throw new ActiveRecordException("Se esta tratando de ejecutar una operacion maliciosa!");
02279 }
02280 }
02281 return $sql_item;
02282 }
02288 protected function exceptions($e)
02289 {
02290 throw $e;
02291 }
02296 public function __toString()
02297 {
02298 return "<" . get_class() . " Object>";
02299 }
02310 public function paginate()
02311 {
02312 $args = func_get_args();
02313 array_unshift($args, $this->source);
02314 if(!class_exists('Paginator')){
02315 require CORE_PATH . 'extensions/db/behaviors/paginate.php';
02316 }
02317 return call_user_func_array(array('Paginator' , 'paginate'), $args);
02318 }
02330 public function paginate_by_sql($sql)
02331 {
02332 $args = func_get_args();
02333 array_unshift($args, $this->source);
02334 if(!class_exists('Paginator')){
02335 require CORE_PATH . 'extensions/db/behaviors/paginate.php';
02336 }
02337 return call_user_func_array(array('Paginator' , 'paginate_by_sql'), $args);
02338 }
02343 public function __sleep()
02344 {
02348 $this->db = null;
02349
02350 return array_keys(get_object_vars($this));
02351 }
02356 public function __wakeup()
02357 {
02361 $this->_connect();
02362 }
02370 public static function get($model)
02371 {
02372 if(isset(self::$_models[$model])) {
02373 return self::$_models[$model];
02374 }
02375
02379 $Model = Util::camelcase(basename($model));
02380
02384 if(!class_exists($Model, false)) {
02388 $file = APP_PATH . "models/$model.php";
02389 if(is_file($file)) {
02390 include $file;
02391 } else {
02392 throw new KumbiaException("No existe el modelo ActiveRecord $model");
02393 }
02394 }
02395
02396 self::$_models[$model] = $obj = new $Model();
02397 return $obj;
02398 }
02399 }