177 protected $_validates = array(
'inclusion_in' => array(),
'exclusion_of' => array(),
'numericality_of' => array(),
178 'format_of' => array(),
'date_in' => array(),
'email_in' => array(),
'uniqueness_of' => array());
263 if (!$this->source) {
270 if (method_exists($this,
'initialize')) {
281 foreach($this->fields as $field) {
282 if (isset($data[$field])) {
283 $this->$field = $data[$field];
295 if (!$this->source) {
337 if ($this->database) {
341 return $core[
'application'][
'database'];
365 if (array_key_exists($mmodel, $this->_belongs_to)) {
366 $relation = $this->_belongs_to[$mmodel];
367 return self::get($relation->model)->find_first($this->{$relation->fk});
368 } elseif (array_key_exists($mmodel, $this->_has_one)) {
369 $relation = $this->_has_one[$mmodel];
370 if ($this->{$this->primary_key[0]}) {
371 return self::get($relation->model)->find_first(
"{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
375 } elseif (array_key_exists($mmodel, $this->_has_many)) {
376 $relation = $this->_has_many[$mmodel];
377 if ($this->{$this->primary_key[0]}) {
378 return self::get($relation->model)->find(
"{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }");
382 } elseif (array_key_exists($mmodel, $this->_has_and_belongs_to_many)) {
383 $relation = $this->_has_and_belongs_to_many[$mmodel];
384 $relation_model = self::get($relation->model);
385 $source = ($this->schema ?
"{$this->schema}." : NULL ) . $this->source;
386 $relation_source = ($relation_model->schema ?
"{$relation_model->schema}." : NULL ) . $relation_model->source;
391 if (!isset($relation->through)) {
392 if (
$source > $relation_source) {
393 $relation->through =
"{$this->source}_{$relation_source}";
395 $relation->through =
"{$relation_source}_{$this->source}";
398 $through = explode(
'/', $relation->through);
399 $relation->through = end($through);
401 if ($this->{$this->primary_key[0]}) {
402 return $relation_model->find_all_by_sql(
"SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source
403 WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]}) }
404 AND {$relation->through}.{$relation->fk} = $relation_source.{$relation_model->primary_key[0]}
405 AND {$relation->through}.{$relation->key} = $source.{$this->primary_key[0]}
406 ORDER BY $relation_source.{$relation_model->primary_key[0]}");
423 if (!$this->_dump_lock) {
424 if (!isset($this->$property)) {
428 return $this->$property;
440 if (!$this->_dump_lock) {
441 if (is_object($value) && is_subclass_of($value,
'KumbiaActiveRecord')) {
442 if (array_key_exists($property, $this->_belongs_to)) {
443 $relation = $this->_belongs_to[$property];
444 $value->dump_model();
445 $this->{$relation->fk} = $value->{$value->primary_key[0]};
447 } elseif (array_key_exists($property, $this->_has_one)) {
448 $relation = $this->_has_one[$property];
449 $value->{$relation->fk} = $this->{$this->primary_key[0]};
452 } elseif ($property ==
"source") {
456 $this->$property = $value;
463 public function __call($method, $args = array())
465 if (substr($method, 0, 8) ==
"find_by_") {
466 $field = substr($method, 8);
468 if (isset($args[0])) {
469 $arg = array(
"conditions: $field = {$this->db->add_quotes($args[0])}");
474 return call_user_func_array(array($this,
"find_first"), array_merge($arg, $args));
476 if (substr($method, 0, 9) ==
"count_by_") {
477 $field = substr($method, 9);
479 if (isset($args[0])) {
480 $arg = array(
"conditions: $field = {$this->db->add_quotes($args[0])}");
485 return call_user_func_array(array($this,
"count"), array_merge($arg, $args));
487 if (substr($method, 0, 12) ==
"find_all_by_") {
488 $field = substr($method, 12);
490 if (isset($args[0])) {
491 $arg = array(
"conditions: $field = {$this->db->add_quotes($args[0])}");
496 return call_user_func_array(array($this,
"find"), array_merge($arg, $args));
498 $model = preg_replace(
'/^get/',
'', $method);
500 if (($data = $this->_get_relation_data($mmodel)) !== FALSE) {
504 if (method_exists($this, $method)) {
505 call_user_func_array(array($this, $method), $args);
507 throw new KumbiaException(
"No existe el método '$method' en ActiveRecord::" . get_class($this));
510 return $this->$method($args);
518 if (!is_object($this->db)) {
521 $this->db->debug = $this->debug;
522 $this->db->logger = $this->logger;
543 if ($this->_dumped) {
548 $this->source = str_replace(
";",
'', strtolower($this->source));
550 $this->_model_name();
551 if (!$this->source) {
555 $table = $this->source;
556 $schema = $this->schema;
557 if (!count(self::get_meta_data($this->source))) {
558 $this->_dumped =
true;
559 $this->_dump_info($table, $schema);
560 if (!count($this->primary_key)) {
561 if (!$this->is_view) {
562 throw new KumbiaException(
"No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad");
566 if (!$this->is_dumped()) {
567 $this->_dumped =
true;
568 $this->_dump_info($table, $schema);
583 $this->_dump_lock =
true;
584 if (!count(self::get_meta_data($table))) {
585 $meta_data = $this->db->describe_table($table, $schema);
587 self::set_meta_data($table, $meta_data);
590 foreach (self::get_meta_data($table) as $field) {
591 $this->fields[] = $field[
'Field'];
592 $aliasAux = $field[
'Field'];
593 if ($field[
'Key'] ==
'PRI') {
594 $this->primary_key[] = $field[
'Field'];
595 $this->alias[$field[
'Field']] =
'Código';
597 $this->non_primary[] = $field[
'Field'];
604 if ($field[
'Null'] ==
'NO' && !(isset($field[
'Default']) && $field[
'Default'])) {
605 $this->not_null[] = $field[
'Field'];
607 if (isset($field[
'Default']) && $field[
'Default']) {
608 $this->_with_default[] = $field[
'Field'];
610 if ($field[
'Type']) {
611 $this->_data_type[$field[
'Field']] = strtolower($field[
'Type']);
613 if (substr($field[
'Field'], strlen($field[
'Field']) - 3, 3) ==
'_at') {
614 $this->_at[] = $field[
'Field'];
615 $aliasAux = substr($field[
'Field'], 0, -3);
617 if (substr($field[
'Field'], strlen($field[
'Field']) - 3, 3) ==
'_in') {
618 $this->_in[] = $field[
'Field'];
619 $aliasAux = substr($field[
'Field'], 0, -3);
621 if (substr($field[
'Field'], strlen($field[
'Field']) - 3, 3) ==
'_id') {
622 $aliasAux = substr($field[
'Field'], 0, -3);
625 $this->alias[$field[
'Field']] = ucwords(strtr($aliasAux,
'_-',
' '));
627 $this->_dump_lock =
false;
639 if ($key && array_key_exists($key, $this->alias)) {
640 return $this->alias[$key];
642 throw new KumbiaException(
"No se pudo obtener el Alias, porque el key: \"$key\" no existe.");
654 if ($key && array_key_exists($key, $this->alias)) {
655 $this->alias[$key] = $value;
657 throw new KumbiaException(
"No se pudo asignar el nuevo valor al Alias, porque el key: \"$key\" no existe.");
668 return $this->db->commit();
678 return $this->db->rollback();
689 return $this->db->begin();
701 foreach ($this->db->fetch_all($sqlQuery) as $result) {
702 $results[] = $this->dump_result($result);
715 $row = $this->db->fetch_one($sqlQuery);
716 if ($row !==
false) {
717 $this->dump_result_self($row);
718 return $this->dump_result($row);
730 public function sql($sqlQuery)
732 return $this->db->query($sqlQuery);
747 if (isset($what[
'columns'])) {
749 } elseif (isset($what[
'distinct'])) {
750 $select.=
'DISTINCT ';
753 $select.= join(
",", $this->fields);
756 $select.=
" FROM {$this->schema}.{$this->source}";
758 $select.=
" FROM {$this->source}";
761 $select.= $this->convert_params_to_sql($what);
764 $result = $this->db->fetch_one($select);
766 $this->dump_result_self($result);
767 $resp = $this->dump_result($result);
787 public function find($what =
'')
791 if (isset($what[
'columns'])) {
793 } elseif (isset($what[
'distinct'])) {
794 $select.=
'DISTINCT ';
797 $select.= join(
",", $this->fields);
800 $select.=
" FROM {$this->schema}.{$this->source}";
802 $select.=
" FROM {$this->source}";
804 $select.= $this->convert_params_to_sql($what);
806 $all_results = $this->db->in_query($select);
807 foreach ($all_results AS $result) {
808 $results[] = $this->dump_result($result);
811 $this->count = count($results, COUNT_NORMAL);
812 if (isset($what[0]) && is_numeric($what[0])) {
813 if (!isset($results[0])) {
817 $this->dump_result_self($all_results[0]);
822 $this->count = count($results, COUNT_NORMAL);
840 if (is_array($what)) {
841 if (!isset($what[
'conditions'])) {
842 if (!isset($this->primary_key[0]) && (isset($this->
id) || $this->is_view)) {
843 $this->primary_key[0] =
"id";
846 if (isset($what[0])) {
847 if (is_numeric($what[0])) {
848 $what[
'conditions'] =
"{$this->primary_key[0]} = ".(int)$what[0] ;
850 if ($what[0] ==
'') {
851 $what[
'conditions'] =
"{$this->primary_key[0]} = ''";
853 $what[
'conditions'] = $what[0];
858 if (isset($what[
'join'])) {
859 $select.=
" {$what['join']}";
861 if (isset($what[
'conditions'])) {
862 $select.=
" WHERE {$what['conditions']}";
864 if (isset($what[
'group'])) {
865 $select.=
" GROUP BY {$what['group']}";
867 if (isset($what[
'having'])) {
868 $select.=
" HAVING {$what['having']}";
870 if (isset($what[
'order'])) {
872 $select.=
" ORDER BY {$what['order']}";
874 $limit_args = array($select);
875 if (isset($what[
'limit'])) {
876 array_push($limit_args,
"limit: ".(
int)$what[
'limit']);
878 if (isset($what[
'offset'])) {
879 array_push($limit_args,
"offset: ".(
int)$what[
'offset']);
881 if (count($limit_args) > 1) {
882 $select = call_user_func_array(array($this,
'limit'), $limit_args);
886 if (is_numeric($what)) {
887 $select.=
"WHERE {$this->primary_key[0]} = ".(int)$what[0] ;
889 $select.=
"WHERE $what";
908 $args = func_get_args();
909 return call_user_func_array(array($this->db,
'limit'), $args);
924 $table = $this->schema .
"." . $this->source;
926 $table = $this->source;
928 if (!isset($what[
'columns'])) {
929 $what[
'columns'] = $what[
'0'];
931 if (!$what[
'columns']) {
932 $what[
'columns'] = $what[
'0'];
936 $select =
"SELECT DISTINCT {$what['columns']} FROM $table ";
941 $select.= $this->convert_params_to_sql($what);
943 foreach ($this->db->fetch_all($select) as $result) {
944 $results[] = $result[0];
958 if (substr(ltrim($sql), 0, 7) !=
"SELECT") {
959 $sql =
"SELECT " . $sql;
961 $num = $db->fetch_one($sql);
975 $table =
"{$this->schema}.{$this->source}";
977 $table = $this->source;
979 unset($what[
'order']);
980 if (isset($what[
'distinct']) && $what[
'distinct']) {
981 if (isset($what[
'group'])) {
982 $select =
"SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table ";
983 $select.= $this->convert_params_to_sql($what);
986 $select =
"SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table ";
987 $select.= $this->convert_params_to_sql($what);
990 $select =
"SELECT COUNT(*) FROM $table ";
991 $select.= $this->convert_params_to_sql($what);
993 $num = $this->db->fetch_one($select);
1006 if (isset($what[
'column'])) {
1007 if (!$what[
'column']) {
1008 $what[
'column'] = $what[0];
1011 $what[
'column'] = $what[0];
1015 if ($this->schema) {
1016 $table =
"{$this->schema}.{$this->source}";
1018 $table = $this->source;
1020 $select =
"SELECT AVG({$what['column']}) FROM $table ";
1021 $select.= $this->convert_params_to_sql($what);
1022 $num = $this->db->fetch_one($select);
1026 public function sum($what =
'')
1029 if (isset($what[
'column'])) {
1030 if (!$what[
'column']) {
1031 $what[
'column'] = $what[0];
1034 $what[
'column'] = $what[0];
1038 if ($this->schema) {
1039 $table =
"{$this->schema}.{$this->source}";
1041 $table = $this->source;
1043 $select =
"SELECT SUM({$what['column']}) FROM $table ";
1044 $select.= $this->convert_params_to_sql($what);
1045 $num = $this->db->fetch_one($select);
1058 if (isset($what[
'column'])) {
1059 if (!$what[
'column']) {
1060 $what[
'column'] = $what[0];
1063 $what[
'column'] = $what[0];
1067 if ($this->schema) {
1068 $table =
"{$this->schema}.{$this->source}";
1070 $table = $this->source;
1072 $select =
"SELECT MAX({$what['column']}) FROM $table ";
1073 $select.= $this->convert_params_to_sql($what);
1074 $num = $this->db->fetch_one($select);
1087 if (isset($what[
'column'])) {
1088 if (!$what[
'column']) {
1089 $what[
'column'] = $what[0];
1092 $what[
'column'] = $what[0];
1096 if ($this->schema) {
1097 $table =
"{$this->schema}.{$this->source}";
1099 $table = $this->source;
1101 $select =
"SELECT MIN({$what['column']}) FROM $table ";
1102 $select.= $this->convert_params_to_sql($what);
1103 $num = $this->db->fetch_one($select);
1115 $num = $this->db->fetch_one($sqlQuery);
1133 if (isset($result[
'type'])) {
1134 if (in_array($result[
'type'], $this->parent_of)) {
1135 if (class_exists($result[
'type'])) {
1136 $obj =
new $result[
'type'];
1137 unset($result[
'type']);
1141 $this->_dump_lock =
true;
1142 if (is_array($result)) {
1143 foreach ($result as $k => $r) {
1144 if (!is_numeric($k)) {
1145 $obj->$k = stripslashes($r);
1149 $this->_dump_lock =
false;
1162 $this->_dump_lock =
true;
1163 if (is_array($result)) {
1164 foreach ($result as $k => $r) {
1165 if (!is_numeric($k)) {
1166 $this->$k = stripslashes($r);
1170 $this->_dump_lock =
false;
1183 $form = $this->source;
1185 return $this->create($_REQUEST[$form]);
1198 $form = $this->source;
1200 return $this->save($_REQUEST[$form]);
1212 $form = $this->source;
1214 return $this->update($_REQUEST[$form]);
1225 if (func_num_args() > 0) {
1227 $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params;
1228 foreach ($this->fields as $field) {
1229 if (isset($values[$field])) {
1230 $this->$field = $values[$field];
1234 if ($this->primary_key[0] ==
'id') {
1237 return $this->save();
1248 if ($this->schema) {
1249 $table =
"{$this->schema}.{$this->source}";
1251 $table = $this->source;
1254 $where_pk = array();
1255 foreach ($this->primary_key as $key) {
1257 $where_pk[] =
" $key = '{$this->$key}'";
1260 if (count($where_pk)) {
1261 $this->_where_pk = join(
" AND ", $where_pk);
1265 $query =
"SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}";
1267 if (is_numeric($where_pk)) {
1268 $query =
"SELECT COUNT(*) FROM $table WHERE {$this->primary_key[0]} = '$where_pk'";
1270 $query =
"SELECT COUNT(*) FROM $table WHERE $where_pk";
1273 $num = $this->db->fetch_one($query);
1285 if (!is_array($values))
1287 foreach ($this->fields as $field) {
1288 if (isset($values[$field])) {
1289 $this->$field = $values[$field];
1293 $ex = $this->exists();
1294 if ($this->schema) {
1295 $table = $this->schema .
"." . $this->source;
1297 $table = $this->source;
1299 #Run Validation Callbacks Before
1300 if (method_exists($this,
'before_validation')) {
1301 if ($this->before_validation() ==
'cancel') {
1305 if (isset($this->before_validation)) {
1306 $method = $this->before_validation;
1307 if ($this->$method() ==
'cancel') {
1313 if (method_exists($this,
"before_validation_on_create")) {
1314 if ($this->before_validation_on_create() ==
'cancel') {
1318 if (isset($this->before_validation_on_create)) {
1319 $method = $this->before_validation_on_create;
1320 if ($this->$method() ==
'cancel') {
1327 if (method_exists($this,
"before_validation_on_update")) {
1328 if ($this->before_validation_on_update() ==
'cancel') {
1332 if (isset($this->before_validation_on_update)) {
1333 $method = $this->before_validation_on_update;
1334 if ($this->$method() ==
'cancel') {
1345 if (isset($this->_validates[
'presence_of'])) {
1346 foreach ($this->_validates[
'presence_of'] as $f => $opt) {
1347 if (isset($this->$f) && (is_null($this->$f) || $this->$f ==
'')) {
1348 if (!$ex && $f == $this->primary_key[0])
1350 if (isset($opt[
'message'])) {
1354 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1355 Flash::error(
"Error: El campo $field no puede ser nulo");
1369 foreach ($this->not_null as $f) {
1370 if (in_array($f, $this->_with_default)) {
1374 if (!isset($this->$f) || is_null($this->$f) || $this->$f ==
'') {
1375 if (!$ex && $f == $this->primary_key[0]) {
1378 if (!$ex && in_array($f, $this->_at)) {
1381 if ($ex && in_array($f, $this->_in)) {
1393 if (isset($this->_validates[
'length_of'])) {
1394 foreach ($this->_validates[
'length_of'] as $f => $opt) {
1395 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1396 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1398 if (strlen($this->$f) < $opt[
'min']) {
1399 if (isset($opt[
'too_short']))
1402 Flash::error(
"Error: El campo $field debe tener como mínimo $opt[min] caracteres");
1406 if (strlen($this->$f) > $opt[
'max']) {
1407 if (isset($opt[
'too_long']))
1410 Flash::error(
"Error: El campo $field debe tener como máximo $opt[max] caracteres");
1421 foreach ($this->_validates[
'inclusion_in'] as $f => $opt) {
1422 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1423 if (!in_array($this->$f, $opt[
'list'])) {
1424 if (isset($opt[
'message'])) {
1427 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1428 Flash::error(
"$field debe tener un valor entre (" . join(
",", $opt[
'list']) .
")");
1439 foreach ($this->_validates[
'exclusion_of'] as $f => $opt) {
1440 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1441 if (in_array($this->$f, $opt[
'list'])) {
1442 if (isset($opt[
'message'])) {
1445 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1446 Flash::error(
"$field no debe tener un valor entre (" . join(
",", $opt[
'list']) .
")");
1457 foreach ($this->_validates[
'numericality_of'] as $f => $opt) {
1458 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1459 if (!is_numeric($this->$f)) {
1460 if (isset($opt[
'message'])) {
1463 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1475 foreach ($this->_validates[
'format_of'] as $f => $opt) {
1476 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1477 if (!filter_var($this->$f, FILTER_VALIDATE_REGEXP, array(
"options" => array(
"regexp" => $opt[
'pattern'])))) {
1478 if (isset($opt[
'message'])) {
1481 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1493 foreach ($this->_validates[
'date_in'] as $f => $opt) {
1494 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1495 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])$/")))) {
1496 if (isset($opt[
'message'])) {
1499 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1511 foreach ($this->_validates[
'email_in'] as $f => $opt) {
1512 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1513 if (!filter_var($this->$f, FILTER_VALIDATE_EMAIL)) {
1514 if (isset($opt[
'message'])) {
1517 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1518 Flash::error(
"Formato de e-mail erroneo en el campo $field");
1533 $and_condition = $ex ?
" AND {$this->primary_key[0]} != '{$this->{$this->primary_key[0]}}'" :
'';
1534 foreach ($this->_validates[
'uniqueness_of'] as $f => $opt) {
1535 if (isset($this->$f) && !is_null($this->$f) && $this->$f !=
'') {
1536 $result = $this->db->fetch_one(
"SELECT COUNT(*) FROM $table WHERE $f = {$this->db->add_quotes($this->$f)} $and_condition");
1538 if (isset($opt[
'message'])) {
1541 $field = isset($opt[
'field']) ? $opt[
'field'] : $f;
1542 Flash::error(
"El valor '{$this->$f}' ya existe para el campo $field");
1549 #Run Validation Callbacks After
1551 if (method_exists($this,
"after_validation_on_create")) {
1552 if ($this->after_validation_on_create() ==
'cancel') {
1556 if (isset($this->after_validation_on_create)) {
1557 $method = $this->after_validation_on_create;
1558 if ($this->$method() ==
'cancel') {
1565 if (method_exists($this,
"after_validation_on_update")) {
1566 if ($this->after_validation_on_update() ==
'cancel') {
1570 if (isset($this->after_validation_on_update)) {
1571 $method = $this->after_validation_on_update;
1572 if ($this->$method() ==
'cancel')
1578 if (method_exists($this,
'after_validation')) {
1579 if ($this->after_validation() ==
'cancel') {
1583 if (isset($this->after_validation)) {
1584 $method = $this->after_validation;
1585 if ($this->$method() ==
'cancel') {
1590 # Run Before Callbacks
1591 if (method_exists($this,
"before_save")) {
1592 if ($this->before_save() ==
'cancel') {
1596 if (isset($this->before_save)) {
1597 $method = $this->before_save;
1598 if ($this->$method() ==
'cancel') {
1604 if (method_exists($this,
"before_update")) {
1605 if ($this->before_update() ==
'cancel') {
1609 if (isset($this->before_update)) {
1610 $method = $this->before_update;
1611 if ($this->$method() ==
'cancel') {
1618 if (method_exists($this,
"before_create")) {
1619 if ($this->before_create() ==
'cancel') {
1623 if (isset($this->before_create)) {
1624 $method = $this->before_create;
1625 if ($this->$method() ==
'cancel') {
1632 $config = $environment[$this->get_database()];
1636 foreach ($this->non_primary as $np) {
1638 if (in_array($np, $this->_in)) {
1639 if (
$config[
'type'] ==
'oracle') {
1640 $this->$np = date(
"Y-m-d");
1642 $this->$np = date(
"Y-m-d G:i:s");
1645 if (isset($this->$np)) {
1647 if (is_null($this->$np) || $this->$np ==
'') {
1653 if ($this->_data_type[$np] ==
'date' &&
$config[
'type'] ==
'oracle') {
1654 $values[] =
"TO_DATE(" . $this->db->add_quotes($this->$np) .
", 'YYYY-MM-DD')";
1656 $values[] = $this->db->add_quotes($this->$np);
1661 $val = $this->db->update($table, $fields, $values, $this->_where_pk);
1665 foreach ($this->fields as $field) {
1666 if ($field != $this->primary_key[0] && !$this->
id) {
1667 if (in_array($field, $this->_at)) {
1668 if (
$config[
'type'] ==
'oracle') {
1669 $this->$field = date(
"Y-m-d");
1671 $this->$field = date(
"Y-m-d G:i:s");
1674 if (in_array($field, $this->_in)) {
1675 unset($this->$field);
1678 if (isset($this->$field) && $this->$field !==
'' && $this->$field !== NULL) {
1681 if (($this->_data_type[$field] ==
'datetime' OR $this->_data_type[$field] ==
'date') && (
$config[
'type'] ==
'mysql' OR
$config[
'type'] ==
'mysqli')) {
1682 $values[] = $this->db->add_quotes(date(
"Y-m-d G:i:s", strtotime($this->$field)));
1683 } elseif ($this->_data_type[$field] ==
'date' &&
$config[
'type'] ==
'oracle') {
1685 $values[] =
"TO_DATE(" . $this->db->add_quotes($this->$field) .
", 'YYYY-MM-DD')";
1687 $values[] = $this->db->add_quotes($this->$field);
1689 } elseif (in_array($field, $this->_with_default)) {
1691 $values[] =
'DEFAULT';
1700 if (
$config[
'type'] ==
'oracle') {
1703 $values[] = $this->source .
"_id_seq.NEXTVAL";
1706 if (
$config[
'type'] ==
'informix') {
1715 $val = $this->db->insert($table, $values, $fields);
1722 $m = $this->db->last_insert_id($table, $this->primary_key[0]);
1723 $this->find_first($m);
1727 if (method_exists($this,
"after_update")) {
1728 if ($this->after_update() ==
'cancel') {
1732 if (isset($this->after_update)) {
1733 $method = $this->after_update;
1734 if ($this->$method() ==
'cancel') {
1741 if (method_exists($this,
"after_create")) {
1742 if ($this->after_create() ==
'cancel') {
1746 if (isset($this->after_create)) {
1747 $method = $this->after_create;
1748 if ($this->$method() ==
'cancel') {
1754 if (method_exists($this,
"after_save")) {
1755 if ($this->after_save() ==
'cancel') {
1759 if (isset($this->after_save)) {
1760 $method = $this->after_save;
1761 if ($this->$method() ==
'cancel') {
1782 return $this->find(
"conditions: $field = {$this->db->add_quotes($value) }");
1793 if (func_num_args() > 0) {
1795 $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params;
1796 foreach ($this->fields as $field) {
1797 if (isset($values[$field])) {
1798 $this->$field = $values[$field];
1802 if ($this->exists()) {
1803 if (method_exists($this,
'before_change')) {
1805 if ($this->before_change($obj->find($this->{$this->primary_key[0]})) ==
'cancel') {
1810 if ($this->save()) {
1811 if (method_exists($this,
'after_change')) {
1812 if ($this->after_change($this) ==
'cancel') {
1819 Flash::error(
'No se puede actualizar porque el registro no existe');
1829 public function delete($what =
'')
1831 if (func_num_args() > 1) {
1834 if ($this->schema) {
1835 $table = $this->schema .
"." . $this->source;
1837 $table = $this->source;
1840 if (is_array($what)) {
1841 if ($what[
"conditions"]) {
1842 $conditions = $what[
"conditions"];
1845 if (is_numeric($what)) {
1847 $conditions =
"{$this->primary_key[0]} = '$what'";
1850 $conditions = $what;
1853 $conditions =
"{$this->primary_key[0]} = '{$this->{$this->primary_key[0]}}'";
1857 if (method_exists($this,
"before_delete")) {
1858 if ($this->{$this->primary_key[0]}) {
1859 $this->find($this->{$this->primary_key[0]});
1861 if ($this->before_delete() ==
'cancel') {
1865 if (isset($this->before_delete)) {
1866 if ($this->{$this->primary_key[0]}) {
1867 $this->find($this->{$this->primary_key[0]});
1869 $method = $this->before_delete;
1870 if ($this->$method() ==
'cancel') {
1875 $val = $this->db->delete($table, $conditions);
1877 if (method_exists($this,
"after_delete")) {
1878 if ($this->after_delete() ==
'cancel') {
1882 if (isset($this->after_delete)) {
1883 $method = $this->after_delete;
1884 if ($this->$method() ==
'cancel') {
1903 if ($this->schema) {
1904 $table = $this->schema .
"." . $this->source;
1906 $table = $this->source;
1908 if (func_num_args() > 1) {
1911 if (!isset($params[
'conditions']) || !$params[
'conditions']) {
1912 if (isset($params[1])) {
1913 $params[
'conditions'] = $params[1];
1915 $params[
'conditions'] =
'';
1918 if ($params[
'conditions']) {
1919 $params[
'conditions'] =
" WHERE " . $params[
'conditions'];
1921 $sql =
"UPDATE $table SET $values {$params['conditions']}";
1922 $limit_args = array($sql);
1923 if (isset($params[
'limit'])) {
1924 array_push($limit_args,
"limit: $params[limit]");
1926 if (isset($params[
'offset'])) {
1927 array_push($limit_args,
"offset: $params[offset]");
1929 if (count($limit_args) > 1) {
1930 $sql = call_user_func_array(array($this,
'limit'), $limit_args);
1933 $config = $environment[$this->get_database()];
1935 if (
$config[
'type'] ==
"informix") {
1936 $this->db->set_return_rows(
false);
1939 return $this->db->query($sql);
1951 if ($this->schema) {
1952 $table = $this->schema .
"." . $this->source;
1954 $table = $this->source;
1956 if (func_num_args() > 1) {
1958 $limit_args = array($select);
1959 if (isset($params[
'limit'])) {
1960 array_push($limit_args,
"limit: $params[limit]");
1962 if (isset($params[
'offset'])) {
1963 array_push($limit_args,
"offset: $params[offset]");
1965 if (count($limit_args) > 1) {
1966 $select = call_user_func_array(array($this,
'limit'), $limit_args);
1969 return $this->db->delete($table, $conditions);
1986 foreach ($this->fields as $field) {
1987 if (!is_array($field)) {
1988 $inspect[] =
"$field: {$this->$field}";
1991 return join(
", ", $inspect);
2011 if (is_string($params))
2014 $this->_validates[
'presence_of'][$field] = $params;
2032 if (is_string($params))
2035 $this->_validates[
'length_of'][$field] = $params;
2036 $this->_validates[
'length_of'][$field][
'min'] = $min;
2037 $this->_validates[
'length_of'][$field][
'max'] = $max;
2052 if (is_string($params))
2055 $this->_validates[
'inclusion_in'][$field] = $params;
2056 $this->_validates[
'inclusion_in'][$field][
'list'] = $list;
2071 if (is_string($params))
2074 $this->_validates[
'exclusion_of'][$field] = $params;
2075 $this->_validates[
'exclusion_of'][$field][
'list'] = $list;
2090 if (is_string($params))
2093 $this->_validates[
'format_of'][$field] = $params;
2094 $this->_validates[
'format_of'][$field][
'pattern'] = $pattern;
2108 if (is_string($params))
2111 $this->_validates[
'numericality_of'][$field] = $params;
2125 if (is_string($params))
2128 $this->_validates[
'email_in'][$field] = $params;
2142 if (is_string($params))
2145 $this->_validates[
'uniqueness_of'][$field] = $params;
2159 if (is_string($params))
2162 $this->_validates[
'date_in'][$field] = $params;
2173 if (strpos(
" " . $this->_data_type[$field],
"int") || strpos(
" " . $this->_data_type[$field],
"decimal") || strpos(
" " . $this->_data_type[$field],
"number")) {
2188 if (isset(self::$models[$table])) {
2189 return self::$models[$table];
2190 } elseif (PRODUCTION) {
2194 return self::$models[$table] = unserialize($metadata);
2209 Cache::driver()->save(serialize($meta_data),
Config::get(
'config.application.metadata_lifetime'), $table,
'kumbia.models');
2211 self::$models[$table] = $meta_data;
2230 for ($i = 0; isset($params[$i]); $i++) {
2232 $index = explode(
'/', $relation);
2233 $index = end($index);
2234 if (!array_key_exists($index, $this->_has_one)) {
2235 $this->_has_one[$index] =
new stdClass();
2236 $this->_has_one[$index]->model = isset($params[
'model']) ? $params[
'model'] : $relation;
2237 $this->_has_one[$index]->fk = isset($params[
'fk']) ? $params[
'fk'] :
Util::smallcase(get_class($this)) .
'_id';
2253 for ($i = 0; isset($params[$i]); $i++) {
2255 $index = explode(
'/', $relation);
2256 $index = end($index);
2257 if (!array_key_exists($index, $this->_belongs_to)) {
2258 $this->_belongs_to[$index] =
new stdClass();
2259 $this->_belongs_to[$index]->model = isset($params[
'model']) ? $params[
'model'] : $relation;
2260 $this->_belongs_to[$index]->fk = isset($params[
'fk']) ? $params[
'fk'] :
"{$relation}_id";
2276 for ($i = 0; isset($params[$i]); $i++) {
2278 $index = explode(
'/', $relation);
2279 $index = end($index);
2280 if (!array_key_exists($index, $this->_has_many)) {
2281 $this->_has_many[$index] =
new stdClass();
2282 $this->_has_many[$index]->model = isset($params[
'model']) ? $params[
'model'] : $relation;
2283 $this->_has_many[$index]->fk = isset($params[
'fk']) ? $params[
'fk'] :
Util::smallcase(get_class($this)) .
'_id';
2301 for ($i = 0; isset($params[$i]); $i++) {
2303 if (!array_key_exists($relation, $this->_has_and_belongs_to_many)) {
2304 $this->_has_and_belongs_to_many[$relation] =
new stdClass();
2305 $this->_has_and_belongs_to_many[$relation]->model = isset($params[
'model']) ? $params[
'model'] : $relation;
2306 $this->_has_and_belongs_to_many[$relation]->fk = isset($params[
'fk']) ? $params[
'fk'] :
"{$relation}_id";
2307 $this->_has_and_belongs_to_many[$relation]->key = isset($params[
'key']) ? $params[
'key'] :
Util::smallcase(get_class($this)) .
'_id';
2308 if (isset($params[
'through'])) {
2309 $this->_has_and_belongs_to_many[$relation]->through = $params[
'through'];
2326 $parents = func_get_args();
2327 foreach ($parents as $parent) {
2328 if (!in_array($parent, $this->parent_of)) {
2329 $this->parent_of[] = $parent;
2342 $sql_item = trim($sql_item);
2343 if ($sql_item !==
'' && $sql_item !== null) {
2344 $sql_temp = preg_replace(
'/\s+/',
'', $sql_item);
2345 if (!preg_match(
'/^[a-zA-Z0-9_\.]+$/', $sql_temp)) {
2346 throw new KumbiaException(
"Se esta tratando de ejecutar una operacion maliciosa!");
2360 $sql_item = trim($sql_item);
2361 if ($sql_item !==
'' && $sql_item !== null) {
2362 $sql_temp = preg_replace(
'/\s+/',
'', $sql_item);
2363 if (!preg_match(
'/^[a-zA-Z_0-9\,\(\)\.\*]+$/', $sql_temp)) {
2364 throw new KumbiaException(
"Se esta tratando de ejecutar una operacion maliciosa!");
2386 return "<" . get_class() .
" Object>";
2401 $args = func_get_args();
2402 array_unshift($args, $this);
2404 require_once
CORE_PATH .
'libs/kumbia_active_record/behaviors/paginate.php';
2406 return call_user_func_array(array(
'Paginator',
'paginate'), $args);
2422 $args = func_get_args();
2423 array_unshift($args, $this);
2425 require_once
CORE_PATH .
'libs/kumbia_active_record/behaviors/paginate.php';
2427 return call_user_func_array(array(
'Paginator',
'paginate_by_sql'), $args);
2441 return array_keys(get_object_vars($this));
2463 public static function get($model)
2465 if (isset(self::$_models[$model])) {
2466 return self::$_models[$model];
2477 if (!class_exists($Model,
false)) {
2481 $file =
APP_PATH .
"models/$model.php";
2482 if (is_file($file)) {
2489 self::$_models[$model] = $obj =
new $Model();
2500 return json_encode($this);
2510 return ((array) $this);
2521 return serialize($this);