| 79 | | |
|---|
| 80 | | //Soportados |
|---|
| 81 | | /** |
|---|
| 82 | | * Resource de conexion a la base de datos |
|---|
| 83 | | * |
|---|
| 84 | | * @var DbBase |
|---|
| 85 | | */ |
|---|
| 86 | | public $db; |
|---|
| 87 | | |
|---|
| 88 | | /** |
|---|
| 89 | | * Modo a utilizar para que el modelo acceda a la bd, utiliza |
|---|
| 90 | | * los modos que se encuentran en environment.ini, por defecto |
|---|
| 91 | | * se considera el definido en config.ini para la aplicacion |
|---|
| 92 | | * |
|---|
| 93 | | * @var string |
|---|
| 94 | | **/ |
|---|
| 95 | | protected $mode; |
|---|
| 96 | | |
|---|
| 97 | | /** |
|---|
| 98 | | * Schema donde esta la tabla |
|---|
| 99 | | * |
|---|
| 100 | | * @var string |
|---|
| 101 | | */ |
|---|
| 102 | | protected $schema; |
|---|
| 103 | | |
|---|
| 104 | | /** |
|---|
| 105 | | * Tabla utilizada para realizar el mapeo |
|---|
| 106 | | * |
|---|
| 107 | | * @var string |
|---|
| 108 | | */ |
|---|
| 109 | | protected $source; |
|---|
| 110 | | |
|---|
| 111 | | /** |
|---|
| 112 | | * Numero de resultados generados en la ultima consulta |
|---|
| 113 | | * |
|---|
| 114 | | * @var integer |
|---|
| 115 | | */ |
|---|
| 116 | | public $count; |
|---|
| 117 | | |
|---|
| 118 | | /** |
|---|
| 119 | | * Nombres de los atributos de la entidad |
|---|
| 120 | | * |
|---|
| 121 | | * @var array |
|---|
| 122 | | */ |
|---|
| 123 | | public $fields = array(); |
|---|
| 124 | | |
|---|
| 125 | | /** |
|---|
| 126 | | * LLaves primarias de la entidad |
|---|
| 127 | | * |
|---|
| 128 | | * @var array |
|---|
| 129 | | */ |
|---|
| 130 | | public $primary_key = array(); |
|---|
| 131 | | |
|---|
| 132 | | /** |
|---|
| 133 | | * Campos que no son llave primaria |
|---|
| 134 | | * |
|---|
| 135 | | * @var array |
|---|
| 136 | | */ |
|---|
| 137 | | public $non_primary = array(); |
|---|
| 138 | | |
|---|
| 139 | | /** |
|---|
| 140 | | * Campos que no permiten nulos |
|---|
| 141 | | * |
|---|
| 142 | | * @var array |
|---|
| 143 | | */ |
|---|
| 144 | | public $not_null = array(); |
|---|
| 145 | | |
|---|
| 146 | | /** |
|---|
| 147 | | * Campos que tienen valor por defecto |
|---|
| 148 | | * |
|---|
| 149 | | * @var array |
|---|
| 150 | | */ |
|---|
| 151 | | private $_with_default = array(); |
|---|
| 152 | | |
|---|
| 153 | | /** |
|---|
| 154 | | * Nombres de atributos, es lo mismo que fields |
|---|
| 155 | | * |
|---|
| 156 | | * @var array |
|---|
| 157 | | */ |
|---|
| 158 | | public $attributes_names = array(); |
|---|
| 159 | | |
|---|
| 160 | | /** |
|---|
| 161 | | * Indica si la clase corresponde a un mapeo de una vista |
|---|
| 162 | | * en la base de datos |
|---|
| 163 | | * |
|---|
| 164 | | * @var boolean |
|---|
| 165 | | */ |
|---|
| 166 | | public $is_view = false; |
|---|
| 167 | | |
|---|
| 168 | | /** |
|---|
| 169 | | * Indica si el modelo esta en modo debug |
|---|
| 170 | | * |
|---|
| 171 | | * @var boolean |
|---|
| 172 | | */ |
|---|
| 173 | | public $debug = false; |
|---|
| 174 | | |
|---|
| 175 | | /** |
|---|
| 176 | | * Indica si se logearan los mensajes generados por la clase |
|---|
| 177 | | * |
|---|
| 178 | | * @var mixed |
|---|
| 179 | | */ |
|---|
| 180 | | public $logger = false; |
|---|
| 181 | | |
|---|
| 182 | | /** |
|---|
| 183 | | * Indica si los datos del modelo deben ser persistidos |
|---|
| 184 | | * |
|---|
| 185 | | * @var boolean |
|---|
| 186 | | */ |
|---|
| 187 | | public $persistent = false; |
|---|
| 188 | | |
|---|
| 189 | | //:Privados |
|---|
| 190 | | /** |
|---|
| 191 | | * Campos que les ser� validado el tama�o |
|---|
| 192 | | * |
|---|
| 193 | | * @var array |
|---|
| 194 | | */ |
|---|
| 195 | | private $_validates_length = array(); |
|---|
| 196 | | |
|---|
| 197 | | /** |
|---|
| 198 | | * Campos que ser�n validados si son numericos |
|---|
| 199 | | * |
|---|
| 200 | | * @var array |
|---|
| 201 | | */ |
|---|
| 202 | | private $_validates_numericality = array(); |
|---|
| 203 | | |
|---|
| 204 | | /** |
|---|
| 205 | | * Campos que seran validados si son email |
|---|
| 206 | | * |
|---|
| 207 | | * @var array |
|---|
| 208 | | */ |
|---|
| 209 | | private $_validates_email = array(); |
|---|
| 210 | | |
|---|
| 211 | | /** |
|---|
| 212 | | * Campos que ser�n validados si son Fecha |
|---|
| 213 | | * |
|---|
| 214 | | * @var array |
|---|
| 215 | | */ |
|---|
| 216 | | private $_validates_date = array(); |
|---|
| 217 | | |
|---|
| 218 | | /** |
|---|
| 219 | | * Campos que seran validados si son unicos |
|---|
| 220 | | * |
|---|
| 221 | | * @var array |
|---|
| 222 | | */ |
|---|
| 223 | | private $_validates_uniqueness = array(); |
|---|
| 224 | | |
|---|
| 225 | | /** |
|---|
| 226 | | * Campos que deberan tener valores dentro de una lista |
|---|
| 227 | | * establecida |
|---|
| 228 | | * |
|---|
| 229 | | * @var array |
|---|
| 230 | | */ |
|---|
| 231 | | private $_validates_inclusion = array(); |
|---|
| 232 | | |
|---|
| 233 | | /** |
|---|
| 234 | | * Campos que deberan tener valores por fuera de una lista |
|---|
| 235 | | * establecida |
|---|
| 236 | | * |
|---|
| 237 | | * @var array |
|---|
| 238 | | */ |
|---|
| 239 | | private $_validates_exclusion = array(); |
|---|
| 240 | | |
|---|
| 241 | | /** |
|---|
| 242 | | * Campos que seran validados contra un formato establecido |
|---|
| 243 | | * |
|---|
| 244 | | * @var array |
|---|
| 245 | | */ |
|---|
| 246 | | private $_validates_format = array(); |
|---|
| 247 | | |
|---|
| 248 | | /** |
|---|
| 249 | | * Campos que son obligatorios, no pueden ser nulos |
|---|
| 250 | | * |
|---|
| 251 | | * @var array |
|---|
| 252 | | */ |
|---|
| 253 | | private $_validates_presence = array(); |
|---|
| 254 | | |
|---|
| 255 | | /** |
|---|
| 256 | | * Campos que terminan en _in |
|---|
| 257 | | * |
|---|
| 258 | | * @var array |
|---|
| 259 | | */ |
|---|
| 260 | | private $_in = array(); |
|---|
| 261 | | |
|---|
| 262 | | /** |
|---|
| 263 | | * Campos que terminan en _at |
|---|
| 264 | | * |
|---|
| 265 | | * @var array |
|---|
| 266 | | */ |
|---|
| 267 | | private $_at = array(); |
|---|
| 268 | | |
|---|
| 269 | | /** |
|---|
| 270 | | * Variable para crear una condicion basada en los |
|---|
| 271 | | * valores del where |
|---|
| 272 | | * |
|---|
| 273 | | * @var string |
|---|
| 274 | | */ |
|---|
| 275 | | private $_where_pk; |
|---|
| 276 | | |
|---|
| 277 | | /** |
|---|
| 278 | | * Indica si ya se han obtenido los metadatos del Modelo |
|---|
| 279 | | * |
|---|
| 280 | | * @var boolean |
|---|
| 281 | | */ |
|---|
| 282 | | private $_dumped = false; |
|---|
| 283 | | |
|---|
| 284 | | /** |
|---|
| 285 | | * Indica si hay bloqueo sobre los warnings cuando una propiedad |
|---|
| 286 | | * del modelo no esta definida- |
|---|
| 287 | | * |
|---|
| 288 | | * @var boolean |
|---|
| 289 | | */ |
|---|
| 290 | | private $_dump_lock = false; |
|---|
| 291 | | |
|---|
| 292 | | /** |
|---|
| 293 | | * Tipos de datos de los campos del modelo |
|---|
| 294 | | * |
|---|
| 295 | | * @var array |
|---|
| 296 | | */ |
|---|
| 297 | | private $_data_type = array(); |
|---|
| 298 | | |
|---|
| 299 | | /** |
|---|
| 300 | | * Relaciones a las cuales tiene una cardinalidad 1-1 |
|---|
| 301 | | * |
|---|
| 302 | | * @var array |
|---|
| 303 | | */ |
|---|
| 304 | | private $_has_one = array(); |
|---|
| 305 | | |
|---|
| 306 | | /** |
|---|
| 307 | | * Relaciones a las cuales tiene una cardinalidad 1-n |
|---|
| 308 | | * |
|---|
| 309 | | * @var array |
|---|
| 310 | | */ |
|---|
| 311 | | private $_has_many = array(); |
|---|
| 312 | | |
|---|
| 313 | | /** |
|---|
| 314 | | * Relaciones a las cuales tiene una cardinalidad 1-1 |
|---|
| 315 | | * |
|---|
| 316 | | * @var array |
|---|
| 317 | | */ |
|---|
| 318 | | private $_belongs_to = array(); |
|---|
| 319 | | |
|---|
| 320 | | /** |
|---|
| 321 | | * Relaciones a las cuales tiene una cardinalidad n-n (muchos a muchos) o 1-n inversa |
|---|
| 322 | | * |
|---|
| 323 | | * @var array |
|---|
| 324 | | */ |
|---|
| 325 | | private $_has_and_belongs_to_many = array(); |
|---|
| 326 | | |
|---|
| 327 | | /** |
|---|
| 328 | | * Clases de las cuales es padre la clase actual |
|---|
| 329 | | * |
|---|
| 330 | | * @var array |
|---|
| 331 | | */ |
|---|
| 332 | | public $parent_of = array(); |
|---|
| 333 | | |
|---|
| 334 | | /** |
|---|
| 335 | | * Persistance Models Meta-data |
|---|
| 336 | | */ |
|---|
| 337 | | static public $models; |
|---|
| 338 | | |
|---|
| 339 | | |
|---|
| 340 | | /** |
|---|
| 341 | | * Constructor del Modelo |
|---|
| 342 | | */ |
|---|
| 343 | | function __construct(){ |
|---|
| 344 | | if(!$this->source){ |
|---|
| 345 | | $this->_model_name(); |
|---|
| 346 | | } |
|---|
| 347 | | |
|---|
| 348 | | /** |
|---|
| 349 | | * Inicializa el modelo en caso de que exista initialize |
|---|
| 350 | | */ |
|---|
| 351 | | if(method_exists($this, "initialize")){ |
|---|
| 352 | | $this->initialize(); |
|---|
| 353 | | } |
|---|
| 354 | | |
|---|
| 355 | | if(func_num_args()){ |
|---|
| 356 | | $params = func_get_args(); |
|---|
| 357 | | if(is_array($params[0])) { |
|---|
| 358 | | $params = $params[0]; |
|---|
| 359 | | } else { |
|---|
| 360 | | $params = get_params($params); |
|---|
| 361 | | } |
|---|
| 362 | | $this->dump_result_self($params); |
|---|
| 363 | | } |
|---|
| 364 | | } |
|---|
| 365 | | |
|---|
| 366 | | /** |
|---|
| 367 | | * Obtiene el nombre de la relacion en el RDBM a partir del nombre de la clase |
|---|
| 368 | | * |
|---|
| 369 | | */ |
|---|
| 370 | | private function _model_name(){ |
|---|
| 371 | | if(!$this->source){ |
|---|
| 372 | | $this->source = uncamelize(lcfirst(get_class($this))); |
|---|
| 373 | | } |
|---|
| 374 | | } |
|---|
| 375 | | |
|---|
| 376 | | /** |
|---|
| 377 | | * Establece publicamente el $source de la tabla |
|---|
| 378 | | * |
|---|
| 379 | | * @param string $source |
|---|
| 380 | | */ |
|---|
| 381 | | public function set_source($source){ |
|---|
| 382 | | $this->source = $source; |
|---|
| 383 | | } |
|---|
| 384 | | |
|---|
| 385 | | /** |
|---|
| 386 | | * Devuelve el source actual |
|---|
| 387 | | * |
|---|
| 388 | | * @return string |
|---|
| 389 | | */ |
|---|
| 390 | | public function get_source(){ |
|---|
| 391 | | return $this->source; |
|---|
| 392 | | } |
|---|
| 393 | | |
|---|
| 394 | | /** |
|---|
| 395 | | * Establece publicamente el $mode del modelo |
|---|
| 396 | | * |
|---|
| 397 | | * @param string $mode |
|---|
| 398 | | */ |
|---|
| 399 | | public function set_mode($mode){ |
|---|
| 400 | | $this->mode = $mode; |
|---|
| 401 | | } |
|---|
| 402 | | |
|---|
| 403 | | /** |
|---|
| 404 | | * Devuelve el mode actual |
|---|
| 405 | | * |
|---|
| 406 | | * @return string |
|---|
| 407 | | */ |
|---|
| 408 | | public function get_mode(){ |
|---|
| 409 | | if($this->mode) { |
|---|
| 410 | | return $this->mode; |
|---|
| 411 | | } else { |
|---|
| 412 | | return Kumbia::get_active_app_mode(); |
|---|
| 413 | | } |
|---|
| 414 | | } |
|---|
| 415 | | |
|---|
| 416 | | /** |
|---|
| 417 | | * Pregunta si el ActiveRecord ya ha consultado la informacion de metadatos |
|---|
| 418 | | * de la base de datos o del registro persistente |
|---|
| 419 | | * |
|---|
| 420 | | * @return boolean |
|---|
| 421 | | */ |
|---|
| 422 | | public function is_dumped(){ |
|---|
| 423 | | return $this->_dumped; |
|---|
| 424 | | } |
|---|
| 425 | | |
|---|
| 426 | | /** |
|---|
| 427 | | * Valida que los valores que sean leidos del objeto ActiveRecord esten definidos |
|---|
| 428 | | * previamente o sean atributos de la entidad |
|---|
| 429 | | * |
|---|
| 430 | | * @param string $property |
|---|
| 431 | | */ |
|---|
| 432 | | function __get($property){ |
|---|
| 433 | | $this->_connect(); |
|---|
| 434 | | if(!$this->_dump_lock){ |
|---|
| 435 | | if(!isset($this->$property)){ |
|---|
| 436 | | if(array_key_exists($property, $this->_belongs_to)){ |
|---|
| 437 | | $relation = $this->_belongs_to[$property]; |
|---|
| 438 | | if(isset(kumbia::$models[$relation->model])) { |
|---|
| 439 | | $assoc_obj = kumbia::$models[$relation->model]->find_first($this->{$relation->fk}); |
|---|
| 440 | | return $assoc_obj; |
|---|
| 441 | | } else { |
|---|
| 442 | | ActiveRecordException::display_warning("Propiedad no definida", "Propiedad indefinida '$property' leida de el modelo '$this->source'", $this->source); |
|---|
| 443 | | return null; |
|---|
| 444 | | } |
|---|
| 445 | | } elseif(array_key_exists($property, $this->_has_one)) { |
|---|
| 446 | | $relation = $this->_has_one[$property]; |
|---|
| 447 | | if(isset(kumbia::$models[$relation->model])) { |
|---|
| 448 | | if($this->{$this->primary_key[0]}) { |
|---|
| 449 | | $assoc_obj = kumbia::$models[$relation->model]->find_first("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]})}"); |
|---|
| 450 | | return $assoc_obj; |
|---|
| 451 | | } else { |
|---|
| 452 | | return null; |
|---|
| 453 | | } |
|---|
| 454 | | } else { |
|---|
| 455 | | ActiveRecordException::display_warning("Propiedad no definida", "Propiedad indefinida '$property' leida de el modelo '$this->source'", $this->source); |
|---|
| 456 | | return null; |
|---|
| 457 | | } |
|---|
| 458 | | } elseif(array_key_exists($property, $this->_has_many)) { |
|---|
| 459 | | $relation = $this->_has_many[$property]; |
|---|
| 460 | | if(isset(kumbia::$models[$relation->model])) { |
|---|
| 461 | | if($this->{$this->primary_key[0]}) { |
|---|
| 462 | | $assoc_objs = kumbia::$models[$relation->model]->find("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]})}"); |
|---|
| 463 | | return $assoc_objs; |
|---|
| 464 | | } else { |
|---|
| 465 | | return array(); |
|---|
| 466 | | } |
|---|
| 467 | | } else { |
|---|
| 468 | | ActiveRecordException::display_warning("Propiedad no definida", "Propiedad indefinida '$property' leida de el modelo '$this->source'", $this->source); |
|---|
| 469 | | return null; |
|---|
| 470 | | } |
|---|
| 471 | | } elseif(array_key_exists($property, $this->_has_and_belongs_to_many)){ |
|---|
| 472 | | $relation = $this->_has_and_belongs_to_many[$property]; |
|---|
| 473 | | if(isset(kumbia::$models[$relation->model])) { |
|---|
| 474 | | $relation_model = kumbia::$models[$relation->model]; |
|---|
| 475 | | $relation_model->connect(); |
|---|
| 476 | | $source = $this->source; |
|---|
| 477 | | $relation_source = $relation_model->source; |
|---|
| 478 | | |
|---|
| 479 | | /** |
|---|
| 480 | | * Cargo atraves de que tabla se efectuara la relacion |
|---|
| 481 | | **/ |
|---|
| 482 | | if(!isset($relation->through)) { |
|---|
| 483 | | if($source > $relation_source) { |
|---|
| 484 | | $relation->through = "{$this->source}_{$relation_source}"; |
|---|
| 485 | | } else { |
|---|
| 486 | | $relation->through = "{$relation_source}_{$this->source}"; |
|---|
| 487 | | } |
|---|
| 488 | | } |
|---|
| 489 | | |
|---|
| 490 | | if($this->{$this->primary_key[0]}) { |
|---|
| 491 | | $assoc_objs = kumbia::$models[$relation->model]->find_all_by_sql("SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source |
|---|
| 492 | | WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]})} |
|---|
| | 69 | //Soportados |
|---|
| | 70 | |
|---|
| | 71 | /** |
|---|
| | 72 | * Resource de conexion a la base de datos |
|---|
| | 73 | * |
|---|
| | 74 | * @var DbBase |
|---|
| | 75 | */ |
|---|
| | 76 | public $db; |
|---|
| | 77 | /** |
|---|
| | 78 | * Modo a utilizar para que el modelo acceda a la bd, utiliza |
|---|
| | 79 | * los modos que se encuentran en environment.ini, por defecto |
|---|
| | 80 | * se considera el definido en config.ini para la aplicacion |
|---|
| | 81 | * |
|---|
| | 82 | * @var string |
|---|
| | 83 | * |
|---|
| | 84 | */ |
|---|
| | 85 | protected $mode; |
|---|
| | 86 | /** |
|---|
| | 87 | * Schema donde esta la tabla |
|---|
| | 88 | * |
|---|
| | 89 | * @var string |
|---|
| | 90 | */ |
|---|
| | 91 | protected $schema; |
|---|
| | 92 | /** |
|---|
| | 93 | * Tabla utilizada para realizar el mapeo |
|---|
| | 94 | * |
|---|
| | 95 | * @var string |
|---|
| | 96 | */ |
|---|
| | 97 | protected $source; |
|---|
| | 98 | /** |
|---|
| | 99 | * Numero de resultados generados en la ultima consulta |
|---|
| | 100 | * |
|---|
| | 101 | * @var integer |
|---|
| | 102 | */ |
|---|
| | 103 | public $count; |
|---|
| | 104 | /** |
|---|
| | 105 | * Nombres de los atributos de la entidad |
|---|
| | 106 | * |
|---|
| | 107 | * @var array |
|---|
| | 108 | */ |
|---|
| | 109 | public $fields = array(); |
|---|
| | 110 | /** |
|---|
| | 111 | * LLaves primarias de la entidad |
|---|
| | 112 | * |
|---|
| | 113 | * @var array |
|---|
| | 114 | */ |
|---|
| | 115 | public $primary_key = array(); |
|---|
| | 116 | /** |
|---|
| | 117 | * Campos que no son llave primaria |
|---|
| | 118 | * |
|---|
| | 119 | * @var array |
|---|
| | 120 | */ |
|---|
| | 121 | public $non_primary = array(); |
|---|
| | 122 | /** |
|---|
| | 123 | * Campos que no permiten nulos |
|---|
| | 124 | * |
|---|
| | 125 | * @var array |
|---|
| | 126 | */ |
|---|
| | 127 | public $not_null = array(); |
|---|
| | 128 | /** |
|---|
| | 129 | * Campos que tienen valor por defecto |
|---|
| | 130 | * |
|---|
| | 131 | * @var array |
|---|
| | 132 | */ |
|---|
| | 133 | private $_with_default = array(); |
|---|
| | 134 | /** |
|---|
| | 135 | * Nombres de atributos, es lo mismo que fields |
|---|
| | 136 | * |
|---|
| | 137 | * @var array |
|---|
| | 138 | */ |
|---|
| | 139 | public $attributes_names = array(); |
|---|
| | 140 | /** |
|---|
| | 141 | * Indica si la clase corresponde a un mapeo de una vista |
|---|
| | 142 | * en la base de datos |
|---|
| | 143 | * |
|---|
| | 144 | * @var boolean |
|---|
| | 145 | */ |
|---|
| | 146 | public $is_view = false; |
|---|
| | 147 | /** |
|---|
| | 148 | * Indica si el modelo esta en modo debug |
|---|
| | 149 | * |
|---|
| | 150 | * @var boolean |
|---|
| | 151 | */ |
|---|
| | 152 | public $debug = false; |
|---|
| | 153 | /** |
|---|
| | 154 | * Indica si se logearan los mensajes generados por la clase |
|---|
| | 155 | * |
|---|
| | 156 | * @var mixed |
|---|
| | 157 | */ |
|---|
| | 158 | public $logger = false; |
|---|
| | 159 | /** |
|---|
| | 160 | * Indica si los datos del modelo deben ser persistidos |
|---|
| | 161 | * |
|---|
| | 162 | * @var boolean |
|---|
| | 163 | */ |
|---|
| | 164 | public $persistent = false; |
|---|
| | 165 | //:Privados |
|---|
| | 166 | |
|---|
| | 167 | /** |
|---|
| | 168 | * Campos que les ser� validado el tama�o |
|---|
| | 169 | * |
|---|
| | 170 | * @var array |
|---|
| | 171 | */ |
|---|
| | 172 | private $_validates_length = array(); |
|---|
| | 173 | /** |
|---|
| | 174 | * Campos que ser�n validados si son numericos |
|---|
| | 175 | * |
|---|
| | 176 | * @var array |
|---|
| | 177 | */ |
|---|
| | 178 | private $_validates_numericality = array(); |
|---|
| | 179 | /** |
|---|
| | 180 | * Campos que seran validados si son email |
|---|
| | 181 | * |
|---|
| | 182 | * @var array |
|---|
| | 183 | */ |
|---|
| | 184 | private $_validates_email = array(); |
|---|
| | 185 | /** |
|---|
| | 186 | * Campos que ser�n validados si son Fecha |
|---|
| | 187 | * |
|---|
| | 188 | * @var array |
|---|
| | 189 | */ |
|---|
| | 190 | private $_validates_date = array(); |
|---|
| | 191 | /** |
|---|
| | 192 | * Campos que seran validados si son unicos |
|---|
| | 193 | * |
|---|
| | 194 | * @var array |
|---|
| | 195 | */ |
|---|
| | 196 | private $_validates_uniqueness = array(); |
|---|
| | 197 | /** |
|---|
| | 198 | * Campos que deberan tener valores dentro de una lista |
|---|
| | 199 | * establecida |
|---|
| | 200 | * |
|---|
| | 201 | * @var array |
|---|
| | 202 | */ |
|---|
| | 203 | private $_validates_inclusion = array(); |
|---|
| | 204 | /** |
|---|
| | 205 | * Campos que deberan tener valores por fuera de una lista |
|---|
| | 206 | * establecida |
|---|
| | 207 | * |
|---|
| | 208 | * @var array |
|---|
| | 209 | */ |
|---|
| | 210 | private $_validates_exclusion = array(); |
|---|
| | 211 | /** |
|---|
| | 212 | * Campos que seran validados contra un formato establecido |
|---|
| | 213 | * |
|---|
| | 214 | * @var array |
|---|
| | 215 | */ |
|---|
| | 216 | private $_validates_format = array(); |
|---|
| | 217 | /** |
|---|
| | 218 | * Campos que son obligatorios, no pueden ser nulos |
|---|
| | 219 | * |
|---|
| | 220 | * @var array |
|---|
| | 221 | */ |
|---|
| | 222 | private $_validates_presence = array(); |
|---|
| | 223 | /** |
|---|
| | 224 | * Campos que terminan en _in |
|---|
| | 225 | * |
|---|
| | 226 | * @var array |
|---|
| | 227 | */ |
|---|
| | 228 | private $_in = array(); |
|---|
| | 229 | /** |
|---|
| | 230 | * Campos que terminan en _at |
|---|
| | 231 | * |
|---|
| | 232 | * @var array |
|---|
| | 233 | */ |
|---|
| | 234 | private $_at = array(); |
|---|
| | 235 | /** |
|---|
| | 236 | * Variable para crear una condicion basada en los |
|---|
| | 237 | * valores del where |
|---|
| | 238 | * |
|---|
| | 239 | * @var string |
|---|
| | 240 | */ |
|---|
| | 241 | private $_where_pk; |
|---|
| | 242 | /** |
|---|
| | 243 | * Indica si ya se han obtenido los metadatos del Modelo |
|---|
| | 244 | * |
|---|
| | 245 | * @var boolean |
|---|
| | 246 | */ |
|---|
| | 247 | private $_dumped = false; |
|---|
| | 248 | /** |
|---|
| | 249 | * Indica si hay bloqueo sobre los warnings cuando una propiedad |
|---|
| | 250 | * del modelo no esta definida- |
|---|
| | 251 | * |
|---|
| | 252 | * @var boolean |
|---|
| | 253 | */ |
|---|
| | 254 | private $_dump_lock = false; |
|---|
| | 255 | /** |
|---|
| | 256 | * Tipos de datos de los campos del modelo |
|---|
| | 257 | * |
|---|
| | 258 | * @var array |
|---|
| | 259 | */ |
|---|
| | 260 | private $_data_type = array(); |
|---|
| | 261 | /** |
|---|
| | 262 | * Relaciones a las cuales tiene una cardinalidad 1-1 |
|---|
| | 263 | * |
|---|
| | 264 | * @var array |
|---|
| | 265 | */ |
|---|
| | 266 | private $_has_one = array(); |
|---|
| | 267 | /** |
|---|
| | 268 | * Relaciones a las cuales tiene una cardinalidad 1-n |
|---|
| | 269 | * |
|---|
| | 270 | * @var array |
|---|
| | 271 | */ |
|---|
| | 272 | private $_has_many = array(); |
|---|
| | 273 | /** |
|---|
| | 274 | * Relaciones a las cuales tiene una cardinalidad 1-1 |
|---|
| | 275 | * |
|---|
| | 276 | * @var array |
|---|
| | 277 | */ |
|---|
| | 278 | private $_belongs_to = array(); |
|---|
| | 279 | /** |
|---|
| | 280 | * Relaciones a las cuales tiene una cardinalidad n-n (muchos a muchos) o 1-n inversa |
|---|
| | 281 | * |
|---|
| | 282 | * @var array |
|---|
| | 283 | */ |
|---|
| | 284 | private $_has_and_belongs_to_many = array(); |
|---|
| | 285 | /** |
|---|
| | 286 | * Clases de las cuales es padre la clase actual |
|---|
| | 287 | * |
|---|
| | 288 | * @var array |
|---|
| | 289 | */ |
|---|
| | 290 | public $parent_of = array(); |
|---|
| | 291 | /** |
|---|
| | 292 | * Persistance Models Meta-data |
|---|
| | 293 | */ |
|---|
| | 294 | static public $models; |
|---|
| | 295 | /** |
|---|
| | 296 | * Constructor del Modelo |
|---|
| | 297 | */ |
|---|
| | 298 | function __construct() { |
|---|
| | 299 | if (!$this->source) { |
|---|
| | 300 | $this->_model_name(); |
|---|
| | 301 | } |
|---|
| | 302 | /** |
|---|
| | 303 | * Inicializa el modelo en caso de que exista initialize |
|---|
| | 304 | */ |
|---|
| | 305 | if (method_exists($this, "initialize")) { |
|---|
| | 306 | $this->initialize(); |
|---|
| | 307 | } |
|---|
| | 308 | if (func_num_args()) { |
|---|
| | 309 | $params = func_get_args(); |
|---|
| | 310 | if (is_array($params[0])) { |
|---|
| | 311 | $params = $params[0]; |
|---|
| | 312 | } else { |
|---|
| | 313 | $params = get_params($params); |
|---|
| | 314 | } |
|---|
| | 315 | $this->dump_result_self($params); |
|---|
| | 316 | } |
|---|
| | 317 | } |
|---|
| | 318 | /** |
|---|
| | 319 | * Obtiene el nombre de la relacion en el RDBM a partir del nombre de la clase |
|---|
| | 320 | * |
|---|
| | 321 | */ |
|---|
| | 322 | private function _model_name() { |
|---|
| | 323 | if (!$this->source) { |
|---|
| | 324 | $this->source = uncamelize(lcfirst(get_class($this))); |
|---|
| | 325 | } |
|---|
| | 326 | } |
|---|
| | 327 | /** |
|---|
| | 328 | * Establece publicamente el $source de la tabla |
|---|
| | 329 | * |
|---|
| | 330 | * @param string $source |
|---|
| | 331 | */ |
|---|
| | 332 | public function set_source($source) { |
|---|
| | 333 | $this->source = $source; |
|---|
| | 334 | } |
|---|
| | 335 | /** |
|---|
| | 336 | * Devuelve el source actual |
|---|
| | 337 | * |
|---|
| | 338 | * @return string |
|---|
| | 339 | */ |
|---|
| | 340 | public function get_source() { |
|---|
| | 341 | return $this->source; |
|---|
| | 342 | } |
|---|
| | 343 | /** |
|---|
| | 344 | * Establece publicamente el $mode del modelo |
|---|
| | 345 | * |
|---|
| | 346 | * @param string $mode |
|---|
| | 347 | */ |
|---|
| | 348 | public function set_mode($mode) { |
|---|
| | 349 | $this->mode = $mode; |
|---|
| | 350 | } |
|---|
| | 351 | /** |
|---|
| | 352 | * Devuelve el mode actual |
|---|
| | 353 | * |
|---|
| | 354 | * @return string |
|---|
| | 355 | */ |
|---|
| | 356 | public function get_mode() { |
|---|
| | 357 | if ($this->mode) { |
|---|
| | 358 | return $this->mode; |
|---|
| | 359 | } else { |
|---|
| | 360 | return Kumbia::get_active_app_mode(); |
|---|
| | 361 | } |
|---|
| | 362 | } |
|---|
| | 363 | /** |
|---|
| | 364 | * Pregunta si el ActiveRecord ya ha consultado la informacion de metadatos |
|---|
| | 365 | * de la base de datos o del registro persistente |
|---|
| | 366 | * |
|---|
| | 367 | * @return boolean |
|---|
| | 368 | */ |
|---|
| | 369 | public function is_dumped() { |
|---|
| | 370 | return $this->_dumped; |
|---|
| | 371 | } |
|---|
| | 372 | /** |
|---|
| | 373 | * Valida que los valores que sean leidos del objeto ActiveRecord esten definidos |
|---|
| | 374 | * previamente o sean atributos de la entidad |
|---|
| | 375 | * |
|---|
| | 376 | * @param string $property |
|---|
| | 377 | */ |
|---|
| | 378 | function __get($property) { |
|---|
| | 379 | $this->_connect(); |
|---|
| | 380 | if (!$this->_dump_lock) { |
|---|
| | 381 | if (!isset($this->$property)) { |
|---|
| | 382 | if (array_key_exists($property, $this->_belongs_to)) { |
|---|
| | 383 | $relation = $this->_belongs_to[$property]; |
|---|
| | 384 | if (isset(kumbia::$models[$relation->model])) { |
|---|
| | 385 | $assoc_obj = kumbia::$models[$relation->model]->find_first($this->{$relation->fk}); |
|---|
| | 386 | return $assoc_obj; |
|---|
| | 387 | } else { |
|---|
| | 388 | ActiveRecordException::display_warning("Propiedad no definida", "Propiedad indefinida '$property' leida de el modelo '$this->source'", $this->source); |
|---|
| | 389 | return null; |
|---|
| | 390 | } |
|---|
| | 391 | } elseif (array_key_exists($property, $this->_has_one)) { |
|---|
| | 392 | $relation = $this->_has_one[$property]; |
|---|
| | 393 | if (isset(kumbia::$models[$relation->model])) { |
|---|
| | 394 | if ($this->{$this->primary_key[0]}) { |
|---|
| | 395 | $assoc_obj = kumbia::$models[$relation->model]->find_first("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }"); |
|---|
| | 396 | return $assoc_obj; |
|---|
| | 397 | } else { |
|---|
| | 398 | return null; |
|---|
| | 399 | } |
|---|
| | 400 | } else { |
|---|
| | 401 | ActiveRecordException::display_warning("Propiedad no definida", "Propiedad indefinida '$property' leida de el modelo '$this->source'", $this->source); |
|---|
| | 402 | return null; |
|---|
| | 403 | } |
|---|
| | 404 | } elseif (array_key_exists($property, $this->_has_many)) { |
|---|
| | 405 | $relation = $this->_has_many[$property]; |
|---|
| | 406 | if (isset(kumbia::$models[$relation->model])) { |
|---|
| | 407 | if ($this->{$this->primary_key[0]}) { |
|---|
| | 408 | $assoc_objs = kumbia::$models[$relation->model]->find("{$relation->fk}={$this->db->add_quotes($this->{$this->primary_key[0]}) }"); |
|---|
| | 409 | return $assoc_objs; |
|---|
| | 410 | } else { |
|---|
| | 411 | return array(); |
|---|
| | 412 | } |
|---|
| | 413 | } else { |
|---|
| | 414 | ActiveRecordException::display_warning("Propiedad no definida", "Propiedad indefinida '$property' leida de el modelo '$this->source'", $this->source); |
|---|
| | 415 | return null; |
|---|
| | 416 | } |
|---|
| | 417 | } elseif (array_key_exists($property, $this->_has_and_belongs_to_many)) { |
|---|
| | 418 | $relation = $this->_has_and_belongs_to_many[$property]; |
|---|
| | 419 | if (isset(kumbia::$models[$relation->model])) { |
|---|
| | 420 | $relation_model = kumbia::$models[$relation->model]; |
|---|
| | 421 | $relation_model->connect(); |
|---|
| | 422 | $source = $this->source; |
|---|
| | 423 | $relation_source = $relation_model->source; |
|---|
| | 424 | /** |
|---|
| | 425 | * Cargo atraves de que tabla se efectuara la relacion |
|---|
| | 426 | * |
|---|
| | 427 | */ |
|---|
| | 428 | if (!isset($relation->through)) { |
|---|
| | 429 | if ($source > $relation_source) { |
|---|
| | 430 | $relation->through = "{$this->source}_{$relation_source}"; |
|---|
| | 431 | } else { |
|---|
| | 432 | $relation->through = "{$relation_source}_{$this->source}"; |
|---|
| | 433 | } |
|---|
| | 434 | } |
|---|
| | 435 | if ($this->{$this->primary_key[0]}) { |
|---|
| | 436 | $assoc_objs = kumbia::$models[$relation->model]->find_all_by_sql("SELECT $relation_source.* FROM $relation_source, {$relation->through}, $source |
|---|
| | 437 | WHERE {$relation->through}.{$relation->key} = {$this->db->add_quotes($this->{$this->primary_key[0]}) } |
|---|
| 644 | | } else { |
|---|
| 645 | | return array(); |
|---|
| 646 | | } |
|---|
| 647 | | } |
|---|
| 648 | | } |
|---|
| 649 | | |
|---|
| 650 | | try { |
|---|
| 651 | | if(method_exists($this, $method)){ |
|---|
| 652 | | call_user_func_array(array($this, $method), $args); |
|---|
| 653 | | } else { |
|---|
| 654 | | if($has_relation){ |
|---|
| 655 | | throw new ActiveRecordException("No existe el modelo '$model' para relacionar con ActiveRecord::{$this->source}"); |
|---|
| 656 | | } else { |
|---|
| 657 | | throw new ActiveRecordException("No existe el método '$method' en ActiveRecord::".get_class($this)); |
|---|
| 658 | | } |
|---|
| 659 | | } |
|---|
| 660 | | } |
|---|
| 661 | | catch(Exception $e){ |
|---|
| 662 | | $this->exceptions($e); |
|---|
| 663 | | } |
|---|
| 664 | | return $this->$method($args); |
|---|
| 665 | | |
|---|
| 666 | | } |
|---|
| 667 | | |
|---|
| 668 | | /** |
|---|
| 669 | | * Se conecta a la base de datos y descarga los meta-datos si es necesario |
|---|
| 670 | | * |
|---|
| 671 | | * @param boolean $new_connection |
|---|
| 672 | | */ |
|---|
| 673 | | private function _connect($new_connection=false){ |
|---|
| 674 | | if(!is_object($this->db)||$new_connection){ |
|---|
| 675 | | if($this->mode) { |
|---|
| 676 | | $this->db = DbBase::raw_connect($new_connection, "mode: {$this->mode}"); |
|---|
| 677 | | } else { |
|---|
| 678 | | $this->db = DbBase::raw_connect($new_connection); |
|---|
| 679 | | } |
|---|
| 680 | | } |
|---|
| 681 | | $this->db->debug = $this->debug; |
|---|
| 682 | | $this->db->logger = $this->logger; |
|---|
| 683 | | $this->dump(); |
|---|
| 684 | | } |
|---|
| 685 | | |
|---|
| 686 | | /** |
|---|
| 687 | | * Cargar los metadatos de la tabla |
|---|
| 688 | | * |
|---|
| 689 | | */ |
|---|
| 690 | | public function dump_model(){ |
|---|
| 691 | | $this->_connect(); |
|---|
| 692 | | } |
|---|
| 693 | | |
|---|
| 694 | | /** |
|---|
| 695 | | * Verifica si la tabla definida en $this->source existe |
|---|
| 696 | | * en la base de datos y la vuelca en dump_info |
|---|
| 697 | | * |
|---|
| 698 | | * @return boolean |
|---|
| 699 | | */ |
|---|
| 700 | | protected function dump(){ |
|---|
| 701 | | if($this->_dumped){ |
|---|
| 702 | | return false; |
|---|
| 703 | | } |
|---|
| 704 | | if($this->source) { |
|---|
| 705 | | $this->source = str_replace(";", "", strtolower($this->source)); |
|---|
| 706 | | } else { |
|---|
| 707 | | $this->_model_name(); |
|---|
| 708 | | if(!$this->source){ |
|---|
| 709 | | return false; |
|---|
| 710 | | } |
|---|
| 711 | | } |
|---|
| 712 | | $table = $this->source; |
|---|
| 713 | | $schema = $this->schema; |
|---|
| 714 | | if(!count(ActiveRecord::get_meta_data($this->source))){ |
|---|
| 715 | | $this->_dumped = true; |
|---|
| 716 | | if($this->db->table_exists($table, $schema)){ |
|---|
| 717 | | $this->_dump_info($table, $schema); |
|---|
| 718 | | } else { |
|---|
| 719 | | throw new ActiveRecordException("No existe la tabla '$table' en la base de datos"); |
|---|
| 720 | | return false; |
|---|
| 721 | | } |
|---|
| 722 | | if(!count($this->primary_key)){ |
|---|
| 723 | | if(!$this->is_view){ |
|---|
| 724 | | throw new ActiveRecordException("No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad"); |
|---|
| 725 | | return false; |
|---|
| 726 | | } |
|---|
| 727 | | } |
|---|
| 728 | | } else { |
|---|
| 729 | | if(!$this->is_dumped()){ |
|---|
| 730 | | $this->_dumped = true; |
|---|
| 731 | | $this->_dump_info($table, $schema); |
|---|
| 732 | | } |
|---|
| 733 | | } |
|---|
| 734 | | return true; |
|---|
| 735 | | } |
|---|
| 736 | | |
|---|
| 737 | | /** |
|---|
| 738 | | * Vuelca la información de la tabla $table en la base de datos |
|---|
| 739 | | * para armar los atributos y meta-data del ActiveRecord |
|---|
| 740 | | * |
|---|
| 741 | | * @param string $table |
|---|
| 742 | | * @return boolean |
|---|
| 743 | | */ |
|---|
| 744 | | private function _dump_info($table, $schema=''){ |
|---|
| 745 | | $this->_dump_lock = true; |
|---|
| 746 | | if(!count(ActiveRecord::get_meta_data($table))){ |
|---|
| 747 | | $meta_data = $this->db->describe_table($table, $schema); |
|---|
| 748 | | if($meta_data){ |
|---|
| 749 | | ActiveRecord::set_meta_data($table, $meta_data); |
|---|
| 750 | | } |
|---|
| 751 | | } |
|---|
| 752 | | foreach(ActiveRecord::get_meta_data($table) as $field){ |
|---|
| 753 | | $this->fields[] = $field['Field']; |
|---|
| 754 | | if($field['Key']=='PRI'){ |
|---|
| 755 | | $this->primary_key[] = $field['Field']; |
|---|
| 756 | | } else $this->non_primary[] = $field['Field']; |
|---|
| 757 | | /** |
|---|
| 758 | | * Si se indica que no puede ser nulo, pero se indica un |
|---|
| 759 | | * valor por defecto, entonces no se incluye en la lista, ya que |
|---|
| 760 | | * al colocar un valor por defecto, el campo nunca sera nulo |
|---|
| 761 | | **/ |
|---|
| 762 | | if($field['Null']=='NO' && !(isset($field['Default']) && $field['Default'])){ |
|---|
| 763 | | $this->not_null[] = $field['Field']; |
|---|
| 764 | | } |
|---|
| 765 | | if(isset($field['Default']) && $field['Default']) { |
|---|
| 766 | | $this->_with_default[] = $field['Field']; |
|---|
| 767 | | } |
|---|
| 768 | | if($field['Type']){ |
|---|
| 769 | | $this->_data_type[$field['Field']] = strtolower($field['Type']); |
|---|
| 770 | | } |
|---|
| 771 | | if(substr($field['Field'], strlen($field['Field'])-3, 3)=='_at'){ |
|---|
| 772 | | $this->_at[] = $field['Field']; |
|---|
| 773 | | } |
|---|
| 774 | | if(substr($field['Field'], strlen($field['Field'])-3, 3)=='_in'){ |
|---|
| 775 | | $this->_in[] = $field['Field']; |
|---|
| 776 | | } |
|---|
| 777 | | } |
|---|
| 778 | | $this->attributes_names = $this->fields; |
|---|
| 779 | | $this->_dump_lock = false; |
|---|
| 780 | | return true; |
|---|
| 781 | | } |
|---|
| 782 | | |
|---|
| 783 | | /** |
|---|
| 784 | | * Commit a Transaction |
|---|
| 785 | | * |
|---|
| 786 | | * @return success |
|---|
| 787 | | */ |
|---|
| 788 | | public function commit(){ |
|---|
| 789 | | $this->_connect(); |
|---|
| 790 | | return $this->db->commit(); |
|---|
| 791 | | } |
|---|
| 792 | | |
|---|
| 793 | | /** |
|---|
| 794 | | * Rollback a Transaction |
|---|
| 795 | | * |
|---|
| 796 | | * @return success |
|---|
| 797 | | */ |
|---|
| 798 | | public function rollback(){ |
|---|
| 799 | | $this->_connect(); |
|---|
| 800 | | return $this->db->rollback(); |
|---|
| 801 | | } |
|---|
| 802 | | |
|---|
| 803 | | /** |
|---|
| 804 | | * Start a transaction in RDBM |
|---|
| 805 | | * |
|---|
| 806 | | * @return success |
|---|
| 807 | | */ |
|---|
| 808 | | public function begin(){ |
|---|
| 809 | | $this->_connect(true); |
|---|
| 810 | | return $this->db->begin(); |
|---|
| 811 | | } |
|---|
| 812 | | |
|---|
| 813 | | /** |
|---|
| 814 | | * Find all records in this table using a SQL Statement |
|---|
| 815 | | * |
|---|
| 816 | | * @param string $sqlQuery |
|---|
| 817 | | * @return ActiveRecord Cursor |
|---|
| 818 | | */ |
|---|
| 819 | | public function find_all_by_sql($sqlQuery){ |
|---|
| 820 | | $this->_connect(); |
|---|
| 821 | | $results = array(); |
|---|
| 822 | | foreach($this->db->fetch_all($sqlQuery) as $result){ |
|---|
| 823 | | $results[] = $this->dump_result($result); |
|---|
| 824 | | } |
|---|
| 825 | | return $results; |
|---|
| 826 | | } |
|---|
| 827 | | /** |
|---|
| 828 | | * Find a record in this table using a SQL Statement |
|---|
| 829 | | * |
|---|
| 830 | | * @param string $sqlQuery |
|---|
| 831 | | * @return ActiveRecord Cursor |
|---|
| 832 | | */ |
|---|
| 833 | | public function find_by_sql($sqlQuery){ |
|---|
| 834 | | $this->_connect(); |
|---|
| 835 | | $row = $this->db->fetch_one($sqlQuery); |
|---|
| 836 | | if($row!==false){ |
|---|
| 837 | | $this->dump_result_self($row); |
|---|
| 838 | | return $this->dump_result($row); |
|---|
| 839 | | } else { |
|---|
| 840 | | return false; |
|---|
| 841 | | } |
|---|
| 842 | | } |
|---|
| 843 | | |
|---|
| 844 | | /** |
|---|
| 845 | | * Execute a SQL Statement directly |
|---|
| 846 | | * |
|---|
| 847 | | * @param string $sqlQuery |
|---|
| 848 | | * @return int affected |
|---|
| 849 | | */ |
|---|
| 850 | | public function sql($sqlQuery){ |
|---|
| 851 | | $this->_connect(); |
|---|
| 852 | | return $this->db->query($sqlQuery); |
|---|
| 853 | | } |
|---|
| 854 | | |
|---|
| 855 | | /** |
|---|
| 856 | | * Return Fist Record |
|---|
| 857 | | * |
|---|
| 858 | | * @param mixed $what |
|---|
| 859 | | * @param boolean $debug |
|---|
| 860 | | * |
|---|
| 861 | | * Recibe los mismos parametros que find |
|---|
| 862 | | * |
|---|
| 863 | | * @return ActiveRecord Cursor |
|---|
| 864 | | */ |
|---|
| 865 | | public function find_first($what=''){ |
|---|
| 866 | | $this->_connect(); |
|---|
| 867 | | $what = get_params(func_get_args()); |
|---|
| 868 | | $select = "SELECT "; |
|---|
| 869 | | if(isset($what['columns'])){ |
|---|
| 870 | | $select.= ActiveRecord::sql_sanizite($what['columns']); |
|---|
| 871 | | } elseif(isset($what['distinct'])) { |
|---|
| 872 | | $select.='DISTINCT '; |
|---|
| 873 | | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
|---|
| 874 | | } else { |
|---|
| 875 | | $select.= join(",", $this->fields); |
|---|
| 876 | | } |
|---|
| 877 | | if($this->schema){ |
|---|
| 878 | | $select.= " FROM {$this->schema}.{$this->source}"; |
|---|
| 879 | | } else { |
|---|
| 880 | | $select.= " FROM {$this->source}"; |
|---|
| 881 | | } |
|---|
| 882 | | |
|---|
| 883 | | $what['limit'] = 1; |
|---|
| 884 | | $select.= $this->convert_params_to_sql($what); |
|---|
| 885 | | $resp = false; |
|---|
| 886 | | try { |
|---|
| 887 | | |
|---|
| 888 | | $result = $this->db->fetch_one($select); |
|---|
| 889 | | if($result){ |
|---|
| 890 | | $this->dump_result_self($result); |
|---|
| 891 | | $resp = $this->dump_result($result); |
|---|
| 892 | | } |
|---|
| 893 | | } |
|---|
| 894 | | catch(Exception $e){ |
|---|
| 895 | | $this->exceptions($e); |
|---|
| 896 | | } |
|---|
| 897 | | return $resp; |
|---|
| 898 | | } |
|---|
| 899 | | |
|---|
| 900 | | /** |
|---|
| 901 | | * Find data on Relational Map table |
|---|
| 902 | | * |
|---|
| 903 | | * @param string $what |
|---|
| 904 | | * @return ActiveRecord Cursor |
|---|
| 905 | | * |
|---|
| 906 | | * columns: columnas a utilizar |
|---|
| 907 | | * conditions : condiciones de busqueda en WHERE |
|---|
| 908 | | * join: inclusion inner join o outer join |
|---|
| 909 | | * group : campo para grupo en GROUP BY |
|---|
| 910 | | * having : condicion para el grupo |
|---|
| 911 | | * order : campo para criterio de ordenamiento ORDER BY |
|---|
| 912 | | * distinct: campos para hacer select distinct |
|---|
| 913 | | */ |
|---|
| 914 | | public function find($what=''){ |
|---|
| 915 | | $this->_connect(); |
|---|
| 916 | | $what = get_params(func_get_args()); |
|---|
| 917 | | $select = "SELECT "; |
|---|
| 918 | | if(isset($what['columns'])){ |
|---|
| 919 | | $select.= $what['columns'] ? ActiveRecord::sql_sanizite($what['columns']) : join(",", $this->fields); |
|---|
| 920 | | } elseif(isset($what['distinct'])) { |
|---|
| 921 | | $select.='DISTINCT '; |
|---|
| 922 | | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
|---|
| 923 | | } else { |
|---|
| 924 | | $select.= join(",", $this->fields); |
|---|
| 925 | | } |
|---|
| 926 | | if($this->schema){ |
|---|
| 927 | | $select.= " FROM {$this->schema}.{$this->source}"; |
|---|
| 928 | | } else { |
|---|
| 929 | | $select.= " FROM {$this->source}"; |
|---|
| 930 | | } |
|---|
| 931 | | $select.= $this->convert_params_to_sql($what); |
|---|
| 932 | | |
|---|
| 933 | | $results = array(); |
|---|
| 934 | | $all_results = $this->db->in_query($select); |
|---|
| 935 | | foreach($all_results AS $result){ |
|---|
| 936 | | $results[] = $this->dump_result($result); |
|---|
| 937 | | } |
|---|
| 938 | | $this->count = count($results); |
|---|
| 939 | | |
|---|
| 940 | | if(isset($what[0]) && is_numeric($what[0])){ |
|---|
| 941 | | if(!isset($results[0])){ |
|---|
| 942 | | $this->count = 0; |
|---|
| 943 | | return false; |
|---|
| 944 | | } else { |
|---|
| 945 | | $this->dump_result_self($all_results[0]); |
|---|
| 946 | | $this->count = 1; |
|---|
| 947 | | return $results[0]; |
|---|
| 948 | | } |
|---|
| 949 | | } else { |
|---|
| 950 | | $this->count = count($results); |
|---|
| 951 | | return $results; |
|---|
| 952 | | } |
|---|
| 953 | | } |
|---|
| 954 | | |
|---|
| 955 | | /* |
|---|
| 956 | | * Arma una consulta SQL con el parametro $what, as�: |
|---|
| 957 | | * $what = get_params(func_get_args()); |
|---|
| 958 | | * $select = "SELECT * FROM Clientes"; |
|---|
| 959 | | * $select.= $this->convert_params_to_sql($what); |
|---|
| 960 | | * |
|---|
| 961 | | * @param string $what |
|---|
| 962 | | * @return string |
|---|
| 963 | | */ |
|---|
| 964 | | public function convert_params_to_sql($what = ''){ |
|---|
| 965 | | $select = ""; |
|---|
| 966 | | if(is_array($what)){ |
|---|
| 967 | | if(!isset($what['conditions'])) { |
|---|
| 968 | | if(!isset($this->primary_key[0]) && (isset($this->id) || $this->is_view)){ |
|---|
| 969 | | $this->primary_key[0] = "id"; |
|---|
| 970 | | } |
|---|
| 971 | | ActiveRecord::sql_item_sanizite($this->primary_key[0]); |
|---|
| 972 | | if(isset($what[0])){ |
|---|
| 973 | | if(is_numeric($what[0])){ |
|---|
| 974 | | $what['conditions'] = "{$this->primary_key[0]} = {$this->db->add_quotes($what[0])}"; |
|---|
| 975 | | } else { |
|---|
| 976 | | if($what[0]==''){ |
|---|
| 977 | | $what['conditions'] = "{$this->primary_key[0]} = ''"; |
|---|
| 978 | | } else { |
|---|
| 979 | | $what['conditions'] = $what[0]; |
|---|
| 980 | | } |
|---|
| 981 | | } |
|---|
| 982 | | } |
|---|
| 983 | | } |
|---|
| 984 | | |
|---|
| 985 | | if(isset($what['join']) && $what['join']){ |
|---|
| 986 | | $select.=" {$what['join']}"; |
|---|
| 987 | | } |
|---|
| 988 | | if(isset($what['conditions']) && $what['conditions']){ |
|---|
| 989 | | $select.=" WHERE {$what['conditions']}"; |
|---|
| 990 | | } |
|---|
| 991 | | if(isset($what['group']) && $what['group']){ |
|---|
| 992 | | $select.=" GROUP BY {$what['group']}"; |
|---|
| 993 | | } |
|---|
| 994 | | if(isset($what['having']) && $what['having']){ |
|---|
| 995 | | $select.=" HAVING {$what['having']}"; |
|---|
| 996 | | } |
|---|
| 997 | | if(isset($what['order'])&&$what['order']) { |
|---|
| 998 | | ActiveRecord::sql_sanizite($what['order']); |
|---|
| 999 | | $select.=" ORDER BY {$what['order']}"; |
|---|
| 1000 | | } |
|---|
| 1001 | | |
|---|
| 1002 | | $limit_args = array($select); |
|---|
| 1003 | | if(isset($what['limit'])) { |
|---|
| 1004 | | array_push($limit_args, "limit: $what[limit]"); |
|---|
| 1005 | | } |
|---|
| 1006 | | if(isset($what['offset'])) { |
|---|
| 1007 | | array_push($limit_args, "offset: $what[offset]"); |
|---|
| 1008 | | } |
|---|
| 1009 | | if(count($limit_args)>1) { |
|---|
| 1010 | | $select = call_user_func_array(array($this,'limit'), $limit_args); |
|---|
| 1011 | | } |
|---|
| 1012 | | |
|---|
| 1013 | | } else { |
|---|
| 1014 | | if(strlen($what)){ |
|---|
| 1015 | | if(is_numeric($what)){ |
|---|
| 1016 | | $select.= "WHERE {$this->primary_key[0]} = '$what'"; |
|---|
| 1017 | | } else { |
|---|
| 1018 | | $select.= "WHERE $what"; |
|---|
| 1019 | | } |
|---|
| 1020 | | } |
|---|
| 1021 | | } |
|---|
| 1022 | | return $select; |
|---|
| 1023 | | } |
|---|
| 1024 | | |
|---|
| 1025 | | /* |
|---|
| 1026 | | * Devuelve una clausula LIMIT adecuada al RDBMS empleado |
|---|
| 1027 | | * |
|---|
| 1028 | | * limit: maxima cantidad de elementos a mostrar |
|---|
| 1029 | | * offset: desde que elemento se comienza a mostrar |
|---|
| 1030 | | * |
|---|
| 1031 | | * @param string $sql consulta select |
|---|
| 1032 | | * @return String clausula LIMIT adecuada al RDBMS empleado |
|---|
| 1033 | | */ |
|---|
| 1034 | | public function limit($sql){ |
|---|
| 1035 | | $args = func_get_args(); |
|---|
| 1036 | | return call_user_func_array(array($this->db, 'limit'), $args); |
|---|
| 1037 | | } |
|---|
| 1038 | | |
|---|
| 1039 | | /** |
|---|
| 1040 | | * Ejecuta un SELECT DISTINCT |
|---|
| 1041 | | * @param string $what |
|---|
| 1042 | | * @return array |
|---|
| 1043 | | * |
|---|
| 1044 | | * Soporta parametros iguales a find |
|---|
| 1045 | | **/ |
|---|
| 1046 | | public function distinct($what=''){ |
|---|
| 1047 | | $this->_connect(); |
|---|
| 1048 | | $what = get_params(func_get_args()); |
|---|
| 1049 | | |
|---|
| 1050 | | if($this->schema){ |
|---|
| 1051 | | $table = $this->schema.".".$this->source; |
|---|
| 1052 | | } else { |
|---|
| 1053 | | $table = $this->source; |
|---|
| 1054 | | } |
|---|
| 1055 | | |
|---|
| 1056 | | if(!isset($what['columns'])){ |
|---|
| 1057 | | $what['columns'] = $what['0']; |
|---|
| 1058 | | } else { |
|---|
| 1059 | | if(!$what['columns']) { |
|---|
| 1060 | | $what['columns'] = $what['0']; |
|---|
| 1061 | | } |
|---|
| 1062 | | } |
|---|
| 1063 | | $what['columns'] = ActiveRecord::sql_sanizite($what['columns']); |
|---|
| 1064 | | |
|---|
| 1065 | | $select = "SELECT DISTINCT {$what['columns']} FROM $table "; |
|---|
| 1066 | | |
|---|
| 1067 | | /** |
|---|
| 1068 | | * Se elimina el de indice cero ya que por defecto convert_params_to_sql lo considera como una condicion en WHERE |
|---|
| 1069 | | */ |
|---|
| 1070 | | unset($what[0]); |
|---|
| 1071 | | $select.= $this->convert_params_to_sql($what); |
|---|
| 1072 | | |
|---|
| 1073 | | $results = array(); |
|---|
| 1074 | | foreach($this->db->fetch_all($select) as $result){ |
|---|
| 1075 | | $results[] = $result[0]; |
|---|
| 1076 | | } |
|---|
| 1077 | | return $results; |
|---|
| 1078 | | } |
|---|
| 1079 | | |
|---|
| 1080 | | /** |
|---|
| 1081 | | * Ejecuta una consulta en el RDBM directamente |
|---|
| 1082 | | * |
|---|
| 1083 | | * @param string $sql |
|---|
| 1084 | | * @return resource |
|---|
| 1085 | | */ |
|---|
| 1086 | | public function select_one($sql){ |
|---|
| 1087 | | $this->_connect(); |
|---|
| 1088 | | if(substr(ltrim($sql), 0, 7)!="SELECT") { |
|---|
| 1089 | | $sql = "SELECT ".$sql; |
|---|
| 1090 | | } |
|---|
| 1091 | | $num = $this->db->fetch_one($sql); |
|---|
| 1092 | | return $num[0]; |
|---|
| 1093 | | } |
|---|
| 1094 | | |
|---|
| 1095 | | static public function static_select_one($sql){ |
|---|
| 1096 | | $db = db::raw_connect(); |
|---|
| 1097 | | if(substr(ltrim($sql), 0, 7)!="SELECT") { |
|---|
| 1098 | | $sql = "SELECT ".$sql; |
|---|
| 1099 | | } |
|---|
| 1100 | | $num = $db->fetch_one($sql); |
|---|
| 1101 | | return $num[0]; |
|---|
| 1102 | | } |
|---|
| 1103 | | |
|---|
| 1104 | | /** |
|---|
| 1105 | | * Realiza un conteo de filas |
|---|
| 1106 | | * |
|---|
| 1107 | | * @param string $what |
|---|
| 1108 | | * @return integer |
|---|
| 1109 | | */ |
|---|
| 1110 | | public function count($what=''){ |
|---|
| 1111 | | $this->_connect(); |
|---|
| 1112 | | $what = get_params(func_get_args()); |
|---|
| 1113 | | |
|---|
| 1114 | | if($this->schema){ |
|---|
| 1115 | | $table = "{$this->schema}.{$this->source}"; |
|---|
| 1116 | | } else { |
|---|
| 1117 | | $table = $this->source; |
|---|
| 1118 | | } |
|---|
| 1119 | | |
|---|
| 1120 | | if(isset($what['distinct'])&&$what['distinct']) { |
|---|
| 1121 | | if(isset($what['group']) || isset($what['order'])) { |
|---|
| 1122 | | $select = "SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table " ; |
|---|
| 1123 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1124 | | $select.=') AS t '; |
|---|
| 1125 | | } else { |
|---|
| 1126 | | $select = "SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table " ; |
|---|
| 1127 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1128 | | } |
|---|
| 1129 | | } else { |
|---|
| 1130 | | $select = "SELECT COUNT(*) FROM $table " ; |
|---|
| 1131 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1132 | | } |
|---|
| 1133 | | |
|---|
| 1134 | | $num = $this->db->fetch_one($select); |
|---|
| 1135 | | return $num[0]; |
|---|
| 1136 | | } |
|---|
| 1137 | | |
|---|
| 1138 | | /** |
|---|
| 1139 | | * Realiza un promedio sobre el campo $what |
|---|
| 1140 | | * |
|---|
| 1141 | | * @param string $what |
|---|
| 1142 | | * @return array |
|---|
| 1143 | | */ |
|---|
| 1144 | | public function average($what=''){ |
|---|
| 1145 | | $this->_connect(); |
|---|
| 1146 | | $what = get_params(func_get_args()); |
|---|
| 1147 | | if(isset($what['column'])) { |
|---|
| 1148 | | if(!$what['column']){ |
|---|
| 1149 | | $what['column'] = $what[0]; |
|---|
| 1150 | | } |
|---|
| 1151 | | } else { |
|---|
| 1152 | | $what['column'] = $what[0]; |
|---|
| 1153 | | } |
|---|
| 1154 | | unset($what[0]); |
|---|
| 1155 | | |
|---|
| 1156 | | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| 1157 | | if($this->schema){ |
|---|
| 1158 | | $table = "{$this->schema}.{$this->source}"; |
|---|
| 1159 | | } else { |
|---|
| 1160 | | $table = $this->source; |
|---|
| 1161 | | } |
|---|
| 1162 | | $select = "SELECT AVG({$what['column']}) FROM $table " ; |
|---|
| 1163 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1164 | | |
|---|
| 1165 | | $num = $this->db->fetch_one($select); |
|---|
| 1166 | | return $num[0]; |
|---|
| 1167 | | } |
|---|
| 1168 | | |
|---|
| 1169 | | public function sum($what=''){ |
|---|
| 1170 | | $this->_connect(); |
|---|
| 1171 | | $what = get_params(func_get_args()); |
|---|
| 1172 | | if(isset($what['column'])) { |
|---|
| 1173 | | if(!$what['column']){ |
|---|
| 1174 | | $what['column'] = $what[0]; |
|---|
| 1175 | | } |
|---|
| 1176 | | } else { |
|---|
| 1177 | | $what['column'] = $what[0]; |
|---|
| 1178 | | } |
|---|
| 1179 | | unset($what[0]); |
|---|
| 1180 | | |
|---|
| 1181 | | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| 1182 | | if($this->schema){ |
|---|
| 1183 | | $table = "{$this->schema}.{$this->source}"; |
|---|
| 1184 | | } else { |
|---|
| 1185 | | $table = $this->source; |
|---|
| 1186 | | } |
|---|
| 1187 | | $select = "SELECT SUM({$what['column']}) FROM $table " ; |
|---|
| 1188 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1189 | | |
|---|
| 1190 | | $num = $this->db->fetch_one($select); |
|---|
| 1191 | | return $num[0]; |
|---|
| 1192 | | } |
|---|
| 1193 | | |
|---|
| 1194 | | /** |
|---|
| 1195 | | * Busca el valor maximo para el campo $what |
|---|
| 1196 | | * |
|---|
| 1197 | | * @param string $what |
|---|
| 1198 | | * @return mixed |
|---|
| 1199 | | */ |
|---|
| 1200 | | public function maximum($what=''){ |
|---|
| 1201 | | $this->_connect(); |
|---|
| 1202 | | $what = get_params(func_get_args()); |
|---|
| 1203 | | if(isset($what['column'])) { |
|---|
| 1204 | | if(!$what['column']){ |
|---|
| 1205 | | $what['column'] = $what[0]; |
|---|
| 1206 | | } |
|---|
| 1207 | | } else { |
|---|
| 1208 | | $what['column'] = $what[0]; |
|---|
| 1209 | | } |
|---|
| 1210 | | unset($what[0]); |
|---|
| 1211 | | |
|---|
| 1212 | | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| 1213 | | if($this->schema){ |
|---|
| 1214 | | $table = "{$this->schema}.{$this->source}"; |
|---|
| 1215 | | } else { |
|---|
| 1216 | | $table = $this->source; |
|---|
| 1217 | | } |
|---|
| 1218 | | $select = "SELECT MAX({$what['column']}) FROM $table " ; |
|---|
| 1219 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1220 | | |
|---|
| 1221 | | $num = $this->db->fetch_one($select); |
|---|
| 1222 | | return $num[0]; |
|---|
| 1223 | | } |
|---|
| 1224 | | |
|---|
| 1225 | | /** |
|---|
| 1226 | | * Busca el valor minimo para el campo $what |
|---|
| 1227 | | * |
|---|
| 1228 | | * @param string $what |
|---|
| 1229 | | * @return mixed |
|---|
| 1230 | | */ |
|---|
| 1231 | | public function minimum($what=''){ |
|---|
| 1232 | | $this->_connect(); |
|---|
| 1233 | | $what = get_params(func_get_args()); |
|---|
| 1234 | | if(isset($what['column'])) { |
|---|
| 1235 | | if(!$what['column']){ |
|---|
| 1236 | | $what['column'] = $what[0]; |
|---|
| 1237 | | } |
|---|
| 1238 | | } else { |
|---|
| 1239 | | $what['column'] = $what[0]; |
|---|
| 1240 | | } |
|---|
| 1241 | | unset($what[0]); |
|---|
| 1242 | | |
|---|
| 1243 | | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| 1244 | | if($this->schema){ |
|---|
| 1245 | | $table = "{$this->schema}.{$this->source}"; |
|---|
| 1246 | | } else { |
|---|
| 1247 | | $table = $this->source; |
|---|
| 1248 | | } |
|---|
| 1249 | | $select = "SELECT MIN({$what['column']}) FROM $table " ; |
|---|
| 1250 | | $select.=$this->convert_params_to_sql($what); |
|---|
| 1251 | | |
|---|
| 1252 | | $num = $this->db->fetch_one($select); |
|---|
| 1253 | | return $num[0]; |
|---|
| 1254 | | } |
|---|
| 1255 | | |
|---|
| 1256 | | /** |
|---|
| 1257 | | * Realiza un conteo directo mediante $sql |
|---|
| 1258 | | * |
|---|
| 1259 | | * @param string $sqlQuery |
|---|
| 1260 | | * @return mixed |
|---|
| 1261 | | */ |
|---|
| 1262 | | public function count_by_sql($sqlQuery){ |
|---|
| 1263 | | $this->_connect(); |
|---|
| 1264 | | $num = $this->db->fetch_one($sqlQuery); |
|---|
| 1265 | | return $num[0]; |
|---|
| 1266 | | } |
|---|
| 1267 | | |
|---|
| 1268 | | /** |
|---|
| 1269 | | * Iguala los valores de un resultado de la base de datos |
|---|
| 1270 | | * en un nuevo objeto con sus correspondientes |
|---|
| 1271 | | * atributos de la clase |
|---|
| 1272 | | * |
|---|
| 1273 | | * @param array $result |
|---|
| 1274 | | * @return ActiveRecord |
|---|
| 1275 | | */ |
|---|
| 1276 | | function dump_result($result){ |
|---|
| 1277 | | $this->_connect(); |
|---|
| 1278 | | $obj = clone $this; |
|---|
| 1279 | | /** |
|---|
| 1280 | | * Consulta si la clase es padre de otra y crea el tipo de dato correcto |
|---|
| 1281 | | */ |
|---|
| 1282 | | if(isset($result['type'])){ |
|---|
| 1283 | | if(in_array($result['type'], $this->parent_of)){ |
|---|
| 1284 | | if(class_exists($result['type'])){ |
|---|
| 1285 | | $obj = new $result['type']; |
|---|
| 1286 | | unset($result['type']); |
|---|
| 1287 | | } |
|---|
| 1288 | | } |
|---|
| 1289 | | } |
|---|
| 1290 | | $this->_dump_lock = true; |
|---|
| 1291 | | if(is_array($result)){ |
|---|
| 1292 | | foreach($result as $k => $r){ |
|---|
| 1293 | | if(!is_numeric($k)){ |
|---|
| 1294 | | $obj->$k = stripslashes($r); |
|---|
| 1295 | | } |
|---|
| 1296 | | } |
|---|
| 1297 | | } |
|---|
| 1298 | | $this->_dump_lock = false; |
|---|
| 1299 | | return $obj; |
|---|
| 1300 | | } |
|---|
| 1301 | | |
|---|
| 1302 | | /** |
|---|
| 1303 | | * Iguala los valores de un resultado de la base de datos |
|---|
| 1304 | | * con sus correspondientes atributos de la clase |
|---|
| 1305 | | * |
|---|
| 1306 | | * @param array $result |
|---|
| 1307 | | * @return ActiveRecord |
|---|
| 1308 | | */ |
|---|
| 1309 | | public function dump_result_self($result){ |
|---|
| 1310 | | $this->_connect(); |
|---|
| 1311 | | $this->_dump_lock = true; |
|---|
| 1312 | | if(is_array($result)){ |
|---|
| 1313 | | foreach($result as $k => $r){ |
|---|
| 1314 | | if(!is_numeric($k)){ |
|---|
| 1315 | | $this->$k = stripslashes($r); |
|---|
| 1316 | | } |
|---|
| 1317 | | } |
|---|
| 1318 | | } |
|---|
| 1319 | | $this->_dump_lock = false; |
|---|
| 1320 | | } |
|---|
| 1321 | | |
|---|
| 1322 | | /** |
|---|
| 1323 | | * Create a new Row using values from $_REQUEST |
|---|
| 1324 | | * |
|---|
| 1325 | | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
|---|
| 1326 | | * @return boolean success |
|---|
| 1327 | | */ |
|---|
| 1328 | | public function create_from_request($form=''){ |
|---|
| 1329 | | if($form) { |
|---|
| 1330 | | return $this->create($_REQUEST[$form]); |
|---|
| 1331 | | } else { |
|---|
| 1332 | | return $this->create($_REQUEST); |
|---|
| 1333 | | } |
|---|
| 1334 | | } |
|---|
| 1335 | | |
|---|
| 1336 | | /** |
|---|
| 1337 | | * Saves a new Row using values from $_REQUEST |
|---|
| 1338 | | * |
|---|
| 1339 | | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
|---|
| 1340 | | * @return boolean success |
|---|
| 1341 | | */ |
|---|
| 1342 | | public function save_from_request($form=''){ |
|---|
| 1343 | | if($form) { |
|---|
| 1344 | | return $this->save($_REQUEST[$form]); |
|---|
| 1345 | | } else { |
|---|
| 1346 | | return $this->save($_REQUEST); |
|---|
| 1347 | | } |
|---|
| 1348 | | } |
|---|
| 1349 | | |
|---|
| 1350 | | |
|---|
| 1351 | | /** |
|---|
| 1352 | | * Updates a Row using values from $_REQUEST |
|---|
| 1353 | | * |
|---|
| 1354 | | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
|---|
| 1355 | | * @return boolean success |
|---|
| 1356 | | */ |
|---|
| 1357 | | public function update_from_request($form=''){ |
|---|
| 1358 | | if($form) { |
|---|
| 1359 | | return $this->update($_REQUEST[$form]); |
|---|
| 1360 | | } else { |
|---|
| 1361 | | return $this->update($_REQUEST); |
|---|
| 1362 | | } |
|---|
| 1363 | | } |
|---|
| 1364 | | |
|---|
| 1365 | | /** |
|---|
| 1366 | | * Creates a new Row in map table |
|---|
| 1367 | | * |
|---|
| 1368 | | * @param mixed $values |
|---|
| 1369 | | * @return success boolean |
|---|
| 1370 | | */ |
|---|
| 1371 | | public function create(){ |
|---|
| 1372 | | $this->_connect(); |
|---|
| 1373 | | |
|---|
| 1374 | | if(func_num_args()>0){ |
|---|
| 1375 | | $params = get_params(func_get_args()); |
|---|
| 1376 | | $values = (isset($params[0]) && is_array($params[0])) ? $params[0]: $params; |
|---|
| 1377 | | foreach($this->fields as $field) { |
|---|
| 1378 | | if(isset($values[$field])) { |
|---|
| 1379 | | $this->$field = $values[$field]; |
|---|
| 1380 | | } |
|---|
| 1381 | | } |
|---|
| 1382 | | } |
|---|
| 1383 | | if($this->primary_key[0]=='id'){ |
|---|
| 1384 | | $this->id = null; |
|---|
| 1385 | | } |
|---|
| 1386 | | |
|---|
| 1387 | | return $this->save(); |
|---|
| 1388 | | } |
|---|
| 1389 | | |
|---|
| 1390 | | /** |
|---|
| 1391 | | * Consulta si un determinado registro existe o no |
|---|
| 1392 | | * en la entidad de la base de datos |
|---|
| 1393 | | * |
|---|
| 1394 | | * @return boolean |
|---|
| 1395 | | */ |
|---|
| 1396 | | function exists($where_pk=''){ |
|---|
| 1397 | | $this->_connect(); |
|---|
| 1398 | | if($this->schema){ |
|---|
| 1399 | | $table = "{$this->schema}.{$this->source}"; |
|---|
| 1400 | | } else { |
|---|
| 1401 | | $table = $this->source; |
|---|
| 1402 | | } |
|---|
| 1403 | | if(!$where_pk){ |
|---|
| 1404 | | $where_pk = array(); |
|---|
| 1405 | | foreach($this->primary_key as $key){ |
|---|
| 1406 | | if($this->$key){ |
|---|
| 1407 | | $where_pk[] = " $key = '{$this->$key}'"; |
|---|
| 1408 | | } |
|---|
| 1409 | | } |
|---|
| 1410 | | if(count($where_pk)){ |
|---|
| 1411 | | $this->_where_pk = join(" AND ", $where_pk); |
|---|
| 1412 | | } else { |
|---|
| 1413 | | return 0; |
|---|
| 1414 | | } |
|---|
| 1415 | | $query = "SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}"; |
|---|
| 1416 | | } else { |
|---|
| 1417 | | if(is_numeric($where_pk)){ |
|---|
| 1418 | | $query = "SELECT(*) FROM $table WHERE id = '$where_pk'"; |
|---|
| 1419 | | } else { |
|---|
| 1420 | | $query = "SELECT COUNT(*) FROM $table WHERE $where_pk"; |
|---|
| 1421 | | } |
|---|
| 1422 | | } |
|---|
| 1423 | | $num = $this->db->fetch_one($query); |
|---|
| 1424 | | return $num[0]; |
|---|
| 1425 | | } |
|---|
| 1426 | | |
|---|
| 1427 | | /** |
|---|
| 1428 | | * Saves Information on the ActiveRecord Properties |
|---|
| 1429 | | * @param array $values array de valores a cargar |
|---|
| 1430 | | * @return boolean success |
|---|
| 1431 | | */ |
|---|
| 1432 | | public function save(){ |
|---|
| 1433 | | $this->_connect(); |
|---|
| 1434 | | |
|---|
| 1435 | | if(func_num_args()>0){ |
|---|
| 1436 | | $params = get_params(func_get_args()); |
|---|
| 1437 | | $values = (isset($params[0]) && is_array($params[0])) ? $params[0]: $params; |
|---|
| 1438 | | foreach($this->fields as $field) { |
|---|
| 1439 | | if(isset($values[$field])) { |
|---|
| 1440 | | $this->$field = $values[$field]; |
|---|
| 1441 | | } |
|---|
| 1442 | | } |
|---|
| 1443 | | } |
|---|
| 1444 | | |
|---|
| 1445 | | $ex = $this->exists(); |
|---|
| 1446 | | |
|---|
| 1447 | | if($this->schema){ |
|---|
| 1448 | | $table = $this->schema.".".$this->source; |
|---|
| 1449 | | } else { |
|---|
| 1450 | | $table = $this->source; |
|---|
| 1451 | | } |
|---|
| 1452 | | |
|---|
| 1453 | | #Run Validation Callbacks Before |
|---|
| 1454 | | if(method_exists($this, 'before_validation')){ |
|---|
| 1455 | | if($this->before_validation()=='cancel') { |
|---|
| 1456 | | return false; |
|---|
| 1457 | | } |
|---|
| 1458 | | } else { |
|---|
| 1459 | | if(isset($this->before_validation)){ |
|---|
| 1460 | | $method = $this->before_validation; |
|---|
| 1461 | | if($this->$method()=='cancel') { |
|---|
| 1462 | | return false; |
|---|
| 1463 | | } |
|---|
| 1464 | | } |
|---|
| 1465 | | } |
|---|
| 1466 | | if(!$ex&&method_exists($this, "before_validation_on_create")){ |
|---|
| 1467 | | if($this->before_validation_on_create()=='cancel') { |
|---|
| 1468 | | return false; |
|---|
| 1469 | | } |
|---|
| 1470 | | } else { |
|---|
| 1471 | | if(isset($this->before_validation_on_create)){ |
|---|
| 1472 | | $method = $this->before_validation_on_create; |
|---|
| 1473 | | if($this->$method()=='cancel') { |
|---|
| 1474 | | return false; |
|---|
| 1475 | | } |
|---|
| 1476 | | } |
|---|
| 1477 | | } |
|---|
| 1478 | | if($ex&&method_exists($this, "before_validation_on_update")){ |
|---|
| 1479 | | if($this->before_validation_on_update()=='cancel') { |
|---|
| 1480 | | return false; |
|---|
| 1481 | | } |
|---|
| 1482 | | } else { |
|---|
| 1483 | | if(isset($this->before_validation_on_update)){ |
|---|
| 1484 | | $method = $this->before_validation_on_update; |
|---|
| 1485 | | if($this->$method()=='cancel') { |
|---|
| 1486 | | return false; |
|---|
| 1487 | | } |
|---|
| 1488 | | } |
|---|
| 1489 | | } |
|---|
| 1490 | | |
|---|
| 1491 | | /** |
|---|
| 1492 | | * Recordamos que aqui no aparecen los que tienen valores por defecto, |
|---|
| 1493 | | * pero sin embargo se debe estar pendiente de validar en las otras verificaciones |
|---|
| 1494 | | * los campos nulos, ya que en estas si el campo es nulo, realmente se refiere a un campo que |
|---|
| 1495 | | * debe tomar el valor por defecto |
|---|
| 1496 | | **/ |
|---|
| 1497 | | $e = false; |
|---|
| 1498 | | for($i=0;$i<=count($this->not_null)-1;$i++){ |
|---|
| 1499 | | $f = $this->not_null[$i]; |
|---|
| 1500 | | if(isset($this->$f) && (is_null($this->$f)||$this->$f=='')){ |
|---|
| 1501 | | if(!$ex&&$f=='id'){ |
|---|
| 1502 | | continue; |
|---|
| 1503 | | } |
|---|
| 1504 | | if(!$ex&&in_array($f, $this->_at)){ |
|---|
| 1505 | | continue; |
|---|
| 1506 | | } |
|---|
| 1507 | | if($ex&&in_array($f, $this->_in)){ |
|---|
| 1508 | | continue; |
|---|
| 1509 | | } |
|---|
| 1510 | | Flash::error("Error: El campo $f no puede ser nulo"); |
|---|
| 1511 | | $e = true; |
|---|
| 1512 | | } |
|---|
| 1513 | | } |
|---|
| 1514 | | if($e){ |
|---|
| 1515 | | return false; |
|---|
| 1516 | | } |
|---|
| 1517 | | |
|---|
| 1518 | | /** |
|---|
| 1519 | | * Validacion validates_presence |
|---|
| 1520 | | **/ |
|---|
| 1521 | | $e = false; |
|---|
| 1522 | | foreach($this->_validates_presence as $f=>$opt){ |
|---|
| 1523 | | $f = $this->not_null[$i]; |
|---|
| 1524 | | if(isset($this->$f) && (is_null($this->$f)||$this->$f=='')){ |
|---|
| 1525 | | if(!$ex&&$f=='id'){ |
|---|
| 1526 | | continue; |
|---|
| 1527 | | } |
|---|
| 1528 | | if(!$ex&&in_array($f, $this->_at)){ |
|---|
| 1529 | | continue; |
|---|
| 1530 | | } |
|---|
| 1531 | | if($ex&&in_array($f, $this->_in)){ |
|---|
| 1532 | | continue; |
|---|
| 1533 | | } |
|---|
| 1534 | | |
|---|
| 1535 | | if(isset($opt['message'])) { |
|---|
| 1536 | | Flash::error($opt['message']); |
|---|
| 1537 | | } else { |
|---|
| 1538 | | $field = isset($opt['field']) ? $opt['field'] : $f; |
|---|
| 1539 | | Flash::error("Error: El campo $field no puede ser nulo"); |
|---|
| 1540 | | } |
|---|
| 1541 | | $e = true; |
|---|
| 1542 | | } |
|---|
| 1543 | | } |
|---|
| 1544 | | if($e){ |
|---|
| 1545 | | return false; |
|---|
| 1546 | | } |
|---|
| 1547 | | |
|---|
| 1548 | | /** |
|---|
| 1549 | | * Validacion validates_length |
|---|
| 1550 | | **/ |
|---|
| 1551 | | $e = false; |
|---|
| 1552 | | foreach($this->_validates_length as $f => $opt){ |
|---|
| 1553 | | if(isset($this->$f) && !is_null($this->$f) && $this->$f!='') { |
|---|
| 1554 | | $field = isset($opt['field']) ? $opt['field'] : $f; |
|---|
| 1555 | | |
|---|
| 1556 | | if(isset($opt['in'])){ |
|---|
| 1557 | | $in = explode(":", $opt['in']); |
|---|
| 1558 | | if(is_numeric($in[0])&&is_numeric($in[1])){ |
|---|
| 1559 | | $opt['minimum'] = $in[0]; |
|---|
| 1560 | | $opt['maximum'] = $in[1]; |
|---|
| 1561 | | } |
|---|
| 1562 | | } |
|---|
| 1563 | | if(isset($opt['minimum']) && is_numeric($opt['minimum'])){ |
|---|
| 1564 | | $n = $opt['minimum']; |
|---|
| 1565 | | if(strlen($this->$f)<$n){ |
|---|
| 1566 | | if(!isset($opt['too_short'])){ |
|---|
| 1567 | | Flash::error("Error: El campo $field debe tener como mínimo $n caracteres"); |
|---|
| 1568 | | $e = true; |
|---|
| 1569 | | } else { |
|---|
| 1570 | | Flash::error($opt['too_short']); |
|---|
| 1571 | | $e = true; |
|---|
| 1572 | | } |
|---|
| 1573 | | } |
|---|
| 1574 | | } |
|---|
| 1575 | | if(isset($opt['maximum']) && is_numeric($opt['maximum'])){ |
|---|
| 1576 | | $n = $opt['maximum']; |
|---|
| 1577 | | if(strlen($this->$f)>$n){ |
|---|
| 1578 | | if(!isset($opt['too_long'])){ |
|---|
| 1579 | | Flash::error("Error: El campo $field debe tener como máximo $n caracteres"); |
|---|
| 1580 | | $e = true; |
|---|
| 1581 | | } else { |
|---|
| 1582 | | Flash::error($opt['too_long']); |
|---|
| 1583 | | $e = true; |
|---|
| 1584 | | } |
|---|
| 1585 | | } |
|---|
| 1586 | | } |
|---|
| 1587 | | } |
|---|
| 1588 | | } |
|---|
| 1589 | | if($e){ |
|---|
| 1590 | | return false; |
|---|
| 1591 | | } |
|---|
| 1592 | | |
|---|
| 1593 | | /** |
|---|
| 1594 | | * Validacion validates_inclusion |
|---|
| 1595 | | **/ |
|---|
| 1596 | | $e = false; |
|---|
| 1597 | | foreach($this->_validates_inclusion as $finc => $list){ |
|---|
| 1598 | | if(isset($this->$finc) && !is_null($this->$finc) && $this->$finc!='') { |
|---|
| 1599 | | if(!is_array($list[1]) && $this->$finc!=$list[1]){ |
|---|
| 1600 | | if(isset($list['message'])) { |
|---|
| 1601 | | Flash::error($list['message']); |
|---|
| 1602 | | } else { |
|---|
| 1603 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
|---|
| 1604 | | Flash::error("$field debe tener un valor entre ({$list[1]})"); |
|---|
| 1605 | | } |
|---|
| 1606 | | $e = true; |
|---|
| 1607 | | } else { |
|---|
| 1608 | | if(!in_array($this->$finc, $list[1])){ |
|---|
| 1609 | | if(isset($list['message'])) { |
|---|
| 1610 | | Flash::error($list['message']); |
|---|
| 1611 | | } else { |
|---|
| 1612 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
|---|
| 1613 | | Flash::error("$field debe tener un valor entre (".join(",", $list[1]).")"); |
|---|
| 1614 | | } |
|---|
| 1615 | | $e = true; |
|---|
| 1616 | | } |
|---|
| 1617 | | } |
|---|
| 1618 | | } |
|---|
| 1619 | | } |
|---|
| 1620 | | if($e){ |
|---|
| 1621 | | return false; |
|---|
| 1622 | | } |
|---|
| 1623 | | |
|---|
| 1624 | | /** |
|---|
| 1625 | | * Validacion validates_exclusion |
|---|
| 1626 | | **/ |
|---|
| 1627 | | $e = false; |
|---|
| 1628 | | foreach($this->_validates_exclusion as $finc => $list){ |
|---|
| 1629 | | if(isset($this->$finc) && !is_null($this->$finc) && $this->$finc!='') { |
|---|
| 1630 | | if(!is_array($list[1]) && $this->$finc==$list[1]){ |
|---|
| 1631 | | if(isset($list['message'])) { |
|---|
| 1632 | | Flash::error($list['message']); |
|---|
| 1633 | | } else { |
|---|
| 1634 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
|---|
| 1635 | | Flash::error("$field no debe tener un valor entre ({$list[1]})"); |
|---|
| 1636 | | } |
|---|
| 1637 | | $e = true; |
|---|
| 1638 | | } else { |
|---|
| 1639 | | if(in_array($this->$finc, $list[1])){ |
|---|
| 1640 | | if(isset($list['message'])) { |
|---|
| 1641 | | Flash::error($list['message']); |
|---|
| 1642 | | } else { |
|---|
| 1643 | | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
|---|
| 1644 | | Flash::error("$field no debe tener un valor entre (".join(",", $list[1]).")"); |
|---|
| 1645 | | } |
|---|
| 1646 | | $e = true; |
|---|
| 1647 | | } |
|---|
| 1648 | | } |
|---|
| 1649 | | } |
|---|
| 1650 | | } |
|---|
| 1651 | | if($e){ |
|---|
| 1652 | | return false; |
|---|
| 1653 | | } |
|---|
| 1654 | | |
|---|
| 1655 | | /** |
|---|
| 1656 | | * Validacion validates_numericality |
|---|
| 1657 | | **/ |
|---|
| 1658 | | $e = false; |
|---|
| 1659 | | foreach($this->_validates_numericality as $fnum=>$opt){ |
|---|
| 1660 | | if(isset($this->$fnum) && !is_null($this->$fnum) && $this->$fnum!='') { |
|---|
| 1661 | | if(!is_numeric($this->$fnum)){ |
|---|
| 1662 | | if(isset($opt['message'])) { |
|---|
| 1663 | | Flash::error($opt['message']); |
|---|
| 1664 | | } else { |
|---|
| 1665 | | $field = isset($opt['field']) ? $opt['field'] : ucwords($fnum); |
|---|
| 1666 | | Flash::error("$field debe tener un valor numérico"); |
|---|
| 1667 | | } |
|---|
| 1668 | | $e = true; |
|---|
| 1669 | | } |
|---|
| 1670 | | } |
|---|
| 1671 | | } |
|---|
| 1672 | | if($e){ |
|---|
| 1673 | | return false; |
|---|
| 1674 | | } |
|---|
| 1675 | | |
|---|
| 1676 | | /** |
|---|
| 1677 | | * Validacion validates_format |
|---|
| 1678 | | **/ |
|---|
| 1679 | | $e = false; |
|---|
| 1680 | | foreach($this->_validates_format as $fkey => $opt){ |
|---|
| 1681 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
|---|
| 1682 | | if(!ereg($opt[1], $this->$fkey)){ |
|---|
| 1683 | | if(isset($opt['message'])) { |
|---|
| 1684 | | Flash::error($opt['message']); |
|---|
| 1685 | | } else { |
|---|
| 1686 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
|---|
| 1687 | | Flash::error("Formato erroneo para $field"); |
|---|
| 1688 | | } |
|---|
| 1689 | | $e = true; |
|---|
| 1690 | | } |
|---|
| 1691 | | } |
|---|
| 1692 | | } |
|---|
| 1693 | | if($e){ |
|---|
| 1694 | | return false; |
|---|
| 1695 | | } |
|---|
| 1696 | | |
|---|
| 1697 | | /** |
|---|
| 1698 | | * Validacion validates_date |
|---|
| 1699 | | **/ |
|---|
| 1700 | | $e = false; |
|---|
| 1701 | | foreach($this->_validates_date as $fkey=>$opt){ |
|---|
| 1702 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
|---|
| 1703 | | if(!ereg("^[0-9]{4}[-/](0[1-9]|1[12])[-/](0[1-9]|[12][0-9]|3[01])$", $this->$fkey, $regs)){ |
|---|
| 1704 | | if(isset($opt['message'])) { |
|---|
| 1705 | | Flash::error($opt['message']); |
|---|
| 1706 | | } else { |
|---|
| 1707 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
|---|
| 1708 | | Flash::error("Formato de fecha ({$this->$fkey}) erroneo para $field"); |
|---|
| 1709 | | } |
|---|
| 1710 | | $e = true; |
|---|
| 1711 | | } |
|---|
| 1712 | | } |
|---|
| 1713 | | } |
|---|
| 1714 | | if($e){ |
|---|
| 1715 | | return false; |
|---|
| 1716 | | } |
|---|
| 1717 | | |
|---|
| 1718 | | /** |
|---|
| 1719 | | * Validacion validates_email |
|---|
| 1720 | | **/ |
|---|
| 1721 | | $e = false; |
|---|
| 1722 | | foreach($this->_validates_email as $fkey){ |
|---|
| 1723 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
|---|
| 1724 | | if(!ereg("^[a-zA-Z0-9_\.\+]+@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*$", $this->$fkey, $regs)){ |
|---|
| 1725 | | if(isset($opt['message'])) { |
|---|
| 1726 | | Flash::error($opt['message']); |
|---|
| 1727 | | } else { |
|---|
| 1728 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
|---|
| 1729 | | Flash::error("Formato de e-mail erroneo en el campo $field"); |
|---|
| 1730 | | } |
|---|
| 1731 | | $e = true; |
|---|
| 1732 | | } |
|---|
| 1733 | | } |
|---|
| 1734 | | } |
|---|
| 1735 | | if($e){ |
|---|
| 1736 | | return false; |
|---|
| 1737 | | } |
|---|
| 1738 | | |
|---|
| 1739 | | /** |
|---|
| 1740 | | * Validacion validates_uniqueness |
|---|
| 1741 | | **/ |
|---|
| 1742 | | $e = false; |
|---|
| 1743 | | foreach($this->_validates_uniqueness as $fkey => $opt){ |
|---|
| 1744 | | ActiveRecord::sql_item_sanizite($fkey); |
|---|
| 1745 | | if(isset($this->$fkey) && !is_null($this->$fkey) && $this->$fkey!='') { |
|---|
| 1746 | | if($ex){ |
|---|
| 1747 | | $copy = clone $this; |
|---|
| 1748 | | $copy->find_first("$fkey = {$this->db->add_quotes($this->$fkey)}"); |
|---|
| 1749 | | $number = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $fkey = {$this->db->add_quotes($this->$fkey)} and $fkey <> {$this->db->add_quotes($copy->$fkey)}"); |
|---|
| 1750 | | } else { |
|---|
| 1751 | | $number = $this->db->fetch_one("SELECT COUNT(*) FROM $table WHERE $fkey = {$this->db->add_quotes($this->$fkey)}"); |
|---|
| 1752 | | } |
|---|
| 1753 | | if((int) $number[0]){ |
|---|
| 1754 | | if(isset($opt['message'])) { |
|---|
| 1755 | | Flash::error($opt['message']); |
|---|
| 1756 | | } else { |
|---|
| 1757 | | $field = isset($opt['field']) ? $opt['field'] : $fkey; |
|---|
| 1758 | | Flash::error("El valor '{$this->$fkey}' ya existe para el campo $field"); |
|---|
| 1759 | | } |
|---|
| 1760 | | $e = true; |
|---|
| 1761 | | } |
|---|
| 1762 | | } |
|---|
| 1763 | | } |
|---|
| 1764 | | if($e){ |
|---|
| 1765 | | return false; |
|---|
| 1766 | | } |
|---|
| 1767 | | |
|---|
| 1768 | | |
|---|
| 1769 | | #Run Validation Callbacks After |
|---|
| 1770 | | if(!$ex&&method_exists($this, "after_validation_on_create")){ |
|---|
| 1771 | | if($this->after_validation_on_create()=='cancel') { |
|---|
| 1772 | | return false; |
|---|
| 1773 | | } |
|---|
| 1774 | | } else { |
|---|
| 1775 | | if(isset($this->after_validation_on_create)){ |
|---|
| 1776 | | $method = $this->after_validation_on_create; |
|---|
| 1777 | | if($this->$method()=='cancel') { |
|---|
| 1778 | | return false; |
|---|
| 1779 | | } |
|---|
| 1780 | | } |
|---|
| 1781 | | } |
|---|
| 1782 | | if($ex&&method_exists($this, "after_validation_on_update")){ |
|---|
| 1783 | | if($this->after_validation_on_update()=='cancel') { |
|---|
| 1784 | | return false; |
|---|
| 1785 | | } |
|---|
| 1786 | | } else { |
|---|
| 1787 | | if(isset($this->after_validation_on_update)){ |
|---|
| 1788 | | $method = $this->after_validation_on_update; |
|---|
| 1789 | | if($this->$method()=='cancel') return false; |
|---|
| 1790 | | } |
|---|
| 1791 | | } |
|---|
| 1792 | | if(method_exists($this, 'after_validation')){ |
|---|
| 1793 | | if($this->after_validation()=='cancel') { |
|---|
| 1794 | | return false; |
|---|
| 1795 | | } |
|---|
| 1796 | | } else { |
|---|
| 1797 | | if(isset($this->after_validation)){ |
|---|
| 1798 | | $method = $this->after_validation; |
|---|
| 1799 | | if($this->$method()=='cancel') { |
|---|
| 1800 | | return false; |
|---|
| 1801 | | } |
|---|
| 1802 | | } |
|---|
| 1803 | | } |
|---|
| 1804 | | # Run Before Callbacks |
|---|
| 1805 | | if(method_exists($this, "before_save")){ |
|---|
| 1806 | | if($this->before_save()=='cancel') { |
|---|
| 1807 | | return false; |
|---|
| 1808 | | } |
|---|
| 1809 | | } else { |
|---|
| 1810 | | if(isset($this->before_save)){ |
|---|
| 1811 | | $method = $this->before_save; |
|---|
| 1812 | | if($this->$method()=='cancel') { |
|---|
| 1813 | | return false; |
|---|
| 1814 | | } |
|---|
| 1815 | | } |
|---|
| 1816 | | } |
|---|
| 1817 | | if($ex&&method_exists($this, "before_update")){ |
|---|
| 1818 | | if($this->before_update()=='cancel') { |
|---|
| 1819 | | return false; |
|---|
| 1820 | | } |
|---|
| 1821 | | } else { |
|---|
| 1822 | | if(isset($this->before_update)){ |
|---|
| 1823 | | $method = $this->before_update; |
|---|
| 1824 | | if($this->$method()=='cancel') { |
|---|
| 1825 | | return false; |
|---|
| 1826 | | } |
|---|
| 1827 | | } |
|---|
| 1828 | | } |
|---|
| 1829 | | if(!$ex&&method_exists($this, "before_create")){ |
|---|
| 1830 | | if($this->before_create()=='cancel') { |
|---|
| 1831 | | return false; |
|---|
| 1832 | | } |
|---|
| 1833 | | } else { |
|---|
| 1834 | | if(isset($this->before_create)){ |
|---|
| 1835 | | $method = $this->before_create; |
|---|
| 1836 | | if($this->$method()=='cancel') { |
|---|
| 1837 | | return false; |
|---|
| 1838 | | } |
|---|
| 1839 | | } |
|---|
| 1840 | | } |
|---|
| 1841 | | |
|---|
| 1842 | | $environment = Config::read("environment.ini"); |
|---|
| 1843 | | $config = $environment->{$this->get_mode()}; |
|---|
| 1844 | | if($ex){ |
|---|
| 1845 | | $fields = array(); |
|---|
| 1846 | | $values = array(); |
|---|
| 1847 | | foreach($this->non_primary as $np){ |
|---|
| 1848 | | $np = ActiveRecord::sql_item_sanizite($np); |
|---|
| 1849 | | if(in_array($np, $this->_in)){ |
|---|
| 1850 | | if($config->database->type=='oracle'){ |
|---|
| 1851 | | $this->$np = date("Y-m-d"); |
|---|
| 1852 | | } else { |
|---|
| 1853 | | $this->$np = date("Y-m-d G:i:s"); |
|---|
| 1854 | | } |
|---|
| 1855 | | } |
|---|
| 1856 | | |
|---|
| 1857 | | if(isset($this->$np)) { |
|---|
| 1858 | | $fields[] = $np; |
|---|
| 1859 | | if(is_null($this->$np) || $this->$np==''){ |
|---|
| 1860 | | $values[] = "NULL"; |
|---|
| 1861 | | } elseif(substr($this->$np, 0, 1)=="%"){ |
|---|
| 1862 | | $values[] = str_replace("%", "", $this->$np); |
|---|
| 1863 | | } else { |
|---|
| 1864 | | /** |
|---|
| 1865 | | * Se debe especificar el formato de fecha en Oracle |
|---|
| 1866 | | */ |
|---|
| 1867 | | if($this->_data_type[$np]=='date'&&$config->database->type=='oracle'){ |
|---|
| 1868 | | $values[] = "TO_DATE(".$this->db->add_quotes($this->$np).", 'YYYY-MM-DD')"; |
|---|
| 1869 | | } else { |
|---|
| 1870 | | $values[] = $this->db->add_quotes($this->$np); |
|---|
| 1871 | | } |
|---|
| 1872 | | } |
|---|
| 1873 | | } |
|---|
| 1874 | | } |
|---|
| 1875 | | $val = $this->db->update($table, $fields, $values, $this->_where_pk); |
|---|
| 1876 | | } else { |
|---|
| 1877 | | $fields = array(); |
|---|
| 1878 | | $values = array(); |
|---|
| 1879 | | foreach($this->fields as $field){ |
|---|
| 1880 | | if($field!='id'&&!$this->id){ |
|---|
| 1881 | | if(in_array($field, $this->_at)){ |
|---|
| 1882 | | if($config->database->type=='oracle'){ |
|---|
| 1883 | | $this->$field = date("Y-m-d"); |
|---|
| 1884 | | } else { |
|---|
| 1885 | | $this->$field = date("Y-m-d G:i:s"); |
|---|
| 1886 | | } |
|---|
| 1887 | | } |
|---|
| 1888 | | |
|---|
| 1889 | | if(in_array($field, $this->_in)){ |
|---|
| 1890 | | unset($this->$field); |
|---|
| 1891 | | } |
|---|
| 1892 | | |
|---|
| 1893 | | $use_default = in_array($field, $this->_with_default) && isset($this->$field) && (is_null($this->$field) || $this->$field==''); |
|---|
| 1894 | | if(isset($this->$field) && !$use_default) { |
|---|
| 1895 | | $fields[] = ActiveRecord::sql_sanizite($field); |
|---|
| 1896 | | if(substr($this->$field, 0, 1)=="%"){ |
|---|
| 1897 | | $values[] = str_replace("%", "", $this->$field); |
|---|
| 1898 | | } else { |
|---|
| 1899 | | if($this->is_a_numeric_type($field)||$this->$field==null){ |
|---|
| 1900 | | $values[] = addslashes($this->$field!==''&&$this->$field!==null ? $this->$field : "NULL"); |
|---|
| 1901 | | } else { |
|---|
| 1902 | | if($this->_data_type[$field]=='date'&&$config->database->type=='oracle'){ |
|---|
| 1903 | | /** |
|---|
| 1904 | | * Se debe especificar el formato de fecha en Oracle |
|---|
| 1905 | | */ |
|---|
| 1906 | | $values[] = "TO_DATE(".$this->db->add_quotes($this->$field).", 'YYYY-MM-DD')"; |
|---|
| 1907 | | } else { |
|---|
| 1908 | | if(!is_null($this->$field) && $this->$field!=''){ |
|---|
| 1909 | | $values[] = $this->db->add_quotes($this->$field); |
|---|
| 1910 | | } else { |
|---|
| 1911 | | $values[] = "NULL"; |
|---|
| 1912 | | } |
|---|
| 1913 | | } |
|---|
| 1914 | | } |
|---|
| 1915 | | } |
|---|
| 1916 | | } |
|---|
| 1917 | | } else { |
|---|
| 1918 | | /** |
|---|
| 1919 | | * Campos autonumericos en Oracle deben utilizar una sequencia auxiliar |
|---|
| 1920 | | */ |
|---|
| 1921 | | if($config->database->type=='oracle'){ |
|---|
| 1922 | | if(!$this->id){ |
|---|
| 1923 | | $fields[] = "id"; |
|---|
| 1924 | | $values[] = $this->source."_id_seq.NEXTVAL"; |
|---|
| 1925 | | } |
|---|
| 1926 | | } |
|---|
| 1927 | | if($config->database->type=='informix'){ |
|---|
| 1928 | | if(!$this->id){ |
|---|
| 1929 | | $fields[] = "id"; |
|---|
| 1930 | | $values[] = 0; |
|---|
| 1931 | | } |
|---|
| 1932 | | } |
|---|
| 1933 | | } |
|---|
| 1934 | | } |
|---|
| 1935 | | $val = $this->db->insert($table, $values, $fields); |
|---|
| 1936 | | } |
|---|
| 1937 | | |
|---|
| 1938 | | if(!isset($config->database->pdo)&&$config->database->type=='oracle'){ |
|---|
| 1939 | | $this->commit(); |
|---|
| 1940 | | } |
|---|
| 1941 | | |
|---|
| 1942 | | if(!$ex){ |
|---|
| 1943 | | $this->db->logger = true; |
|---|
| 1944 | | $m = $this->db->last_insert_id($table, $this->primary_key[0]); |
|---|
| 1945 | | $this->find_first($m); |
|---|
| 1946 | | } |
|---|
| 1947 | | |
|---|
| 1948 | | if($val){ |
|---|
| 1949 | | if($ex&&method_exists($this, "after_update")){ |
|---|
| 1950 | | if($this->after_update()=='cancel') { |
|---|
| 1951 | | return false; |
|---|
| 1952 | | } |
|---|
| 1953 | | } else { |
|---|
| 1954 | | if(isset($this->after_update)){ |
|---|
| 1955 | | $method = $this->after_update; |
|---|
| 1956 | | if($this->$method()=='cancel') { |
|---|
| 1957 | | return false; |
|---|
| 1958 | | } |
|---|
| 1959 | | } |
|---|
| 1960 | | } |
|---|
| 1961 | | if(!$ex&&method_exists($this, "after_create")){ |
|---|
| 1962 | | if($this->after_create()=='cancel') { |
|---|
| 1963 | | return false; |
|---|
| 1964 | | } |
|---|
| 1965 | | } else { |
|---|
| 1966 | | if(isset($this->after_create)){ |
|---|
| 1967 | | $method = $this->after_create; |
|---|
| 1968 | | if($this->$method()=='cancel') { |
|---|
| 1969 | | return false; |
|---|
| 1970 | | } |
|---|
| 1971 | | } |
|---|
| 1972 | | } |
|---|
| 1973 | | if(method_exists($this, "after_save")){ |
|---|
| 1974 | | if($this->after_save()=='cancel') { |
|---|
| 1975 | | return false; |
|---|
| 1976 | | } |
|---|
| 1977 | | } else { |
|---|
| 1978 | | if(isset($this->after_save)){ |
|---|
| 1979 | | $method = $this->after_save; |
|---|
| 1980 | | if($this->$method()=='cancel') { |
|---|
| 1981 | | return false; |
|---|
| 1982 | | } |
|---|
| 1983 | | } |
|---|
| 1984 | | } |
|---|
| 1985 | | return $val; |
|---|
| 1986 | | } else { |
|---|
| 1987 | | return false; |
|---|
| 1988 | | } |
|---|
| 1989 | | } |
|---|
| 1990 | | |
|---|
| 1991 | | /** |
|---|
| 1992 | | * Find All data in the Relational Table |
|---|
| 1993 | | * |
|---|
| 1994 | | * @param string $field |
|---|
| 1995 | | * @param string $value |
|---|
| 1996 | | * @return ActiveRecod Cursor |
|---|
| 1997 | | */ |
|---|
| 1998 | | function find_all_by($field, $value){ |
|---|
| 1999 | | ActiveRecord::sql_item_sanizite($field); |
|---|
| 2000 | | return $this->find("conditions: $field = {$this->db->add_quotes($value)}"); |
|---|
| 2001 | | } |
|---|
| 2002 | | |
|---|
| 2003 | | /** |
|---|
| 2004 | | * Updates Data in the Relational Table |
|---|
| 2005 | | * |
|---|
| 2006 | | * @param mixed $values |
|---|
| 2007 | | * @return boolean sucess |
|---|
| 2008 | | */ |
|---|
| 2009 | | function update(){ |
|---|
| 2010 | | $this->_connect(); |
|---|
| 2011 | | if(func_num_args()>0){ |
|---|
| 2012 | | $params = get_params(func_get_args()); |
|---|
| 2013 | | $values = (isset($params[0]) && is_array($params[0])) ? $params[0]: $params; |
|---|
| 2014 | | foreach($this->fields as $field) { |
|---|
| 2015 | | if(isset($values[$field])) { |
|---|
| 2016 | | $this->$field = $values[$field]; |
|---|
| 2017 | | } |
|---|
| 2018 | | } |
|---|
| 2019 | | } |
|---|
| 2020 | | |
|---|
| 2021 | | if($this->exists()){ |
|---|
| 2022 | | return $this->save(); |
|---|
| 2023 | | } else { |
|---|
| 2024 | | Flash::error('No se puede actualizar porque el registro no existe'); |
|---|
| 2025 | | return false; |
|---|
| 2026 | | } |
|---|
| 2027 | | } |
|---|
| 2028 | | |
|---|
| 2029 | | /** |
|---|
| 2030 | | * Deletes data from Relational Map Table |
|---|
| 2031 | | * |
|---|
| 2032 | | * @param mixed $what |
|---|
| 2033 | | */ |
|---|
| 2034 | | function delete($what=''){ |
|---|
| 2035 | | $this->_connect(); |
|---|
| 2036 | | if(func_num_args()>1){ |
|---|
| 2037 | | $what = get_params(func_get_args()); |
|---|
| 2038 | | } |
|---|
| 2039 | | if($this->schema){ |
|---|
| 2040 | | $table = $this->schema.".".$this->source; |
|---|
| 2041 | | } else { |
|---|
| 2042 | | $table = $this->source; |
|---|
| 2043 | | } |
|---|
| 2044 | | $conditions = ""; |
|---|
| 2045 | | if(is_array($what)){ |
|---|
| 2046 | | if($what["conditions"]) { |
|---|
| 2047 | | $conditions = $what["conditions"]; |
|---|
| 2048 | | } |
|---|
| 2049 | | } else { |
|---|
| 2050 | | if(is_numeric($what)){ |
|---|
| 2051 | | ActiveRecord::sql_sanizite($this->primary_key[0]); |
|---|
| 2052 | | $conditions = "{$this->primary_key[0]} = '$what'"; |
|---|
| 2053 | | } else{ |
|---|
| 2054 | | if($what){ |
|---|
| 2055 | | $conditions = $what; |
|---|
| 2056 | | } else { |
|---|
| 2057 | | ActiveRecord::sql_sanizite($this->primary_key[0]); |
|---|
| 2058 | | $conditions = "{$this->primary_key[0]} = '{$this->{$this->primary_key[0]}}'"; |
|---|
| 2059 | | } |
|---|
| 2060 | | } |
|---|
| 2061 | | } |
|---|
| 2062 | | if(method_exists($this, "before_delete")){ |
|---|
| 2063 | | if($this->id) { |
|---|
| 2064 | | $this->find($this->id); |
|---|
| 2065 | | } |
|---|
| 2066 | | if($this->before_delete()=='cancel') { |
|---|
| 2067 | | return false; |
|---|
| 2068 | | } |
|---|
| 2069 | | } else { |
|---|
| 2070 | | if(isset($this->before_delete)){ |
|---|
| 2071 | | if($this->id) { |
|---|
| 2072 | | $this->find($this->id); |
|---|
| 2073 | | } |
|---|
| 2074 | | $method = $this->before_delete; |
|---|
| 2075 | | if($this->$method()=='cancel') { |
|---|
| 2076 | | return false; |
|---|
| 2077 | | } |
|---|
| 2078 | | } |
|---|
| 2079 | | } |
|---|
| 2080 | | $val = $this->db->delete($table, $conditions); |
|---|
| 2081 | | if($val){ |
|---|
| 2082 | | if(method_exists($this, "after_delete")){ |
|---|
| 2083 | | if($this->after_delete()=='cancel') { |
|---|
| 2084 | | return false; |
|---|
| 2085 | | } |
|---|
| 2086 | | } else { |
|---|
| 2087 | | if(isset($this->after_delete)){ |
|---|
| 2088 | | $method = $this->after_delete; |
|---|
| 2089 | | if($this->$method()=='cancel') { |
|---|
| 2090 | | return false; |
|---|
| 2091 | | } |
|---|
| 2092 | | } |
|---|
| 2093 | | } |
|---|
| 2094 | | } |
|---|
| 2095 | | return $val; |
|---|
| 2096 | | } |
|---|
| 2097 | | |
|---|
| 2098 | | /** |
|---|
| 2099 | | * Actualiza todos los atributos de la entidad |
|---|
| 2100 | | * $Clientes->update_all("estado='A', fecha='2005-02-02'", "id>100"); |
|---|
| 2101 | | * $Clientes->update_all("estado='A', fecha='2005-02-02'", "id>100", "limit: 10"); |
|---|
| 2102 | | * |
|---|
| 2103 | | * @param string $values |
|---|
| 2104 | | */ |
|---|
| 2105 | | function update_all($values){ |
|---|
| 2106 | | $this->_connect(); |
|---|
| 2107 | | $params = array(); |
|---|
| 2108 | | if($this->schema){ |
|---|
| 2109 | | $table = $this->schema.".".$this->source; |
|---|
| 2110 | | } else { |
|---|
| 2111 | | $table = $this->source; |
|---|
| 2112 | | } |
|---|
| 2113 | | if(func_num_args()>1){ |
|---|
| 2114 | | $params = get_params(func_get_args()); |
|---|
| 2115 | | } |
|---|
| 2116 | | if(!isset($params['conditions'])||!$params['conditions']){ |
|---|
| 2117 | | if(isset($params[1])){ |
|---|
| 2118 | | $params['conditions'] = $params[1]; |
|---|
| 2119 | | } else { |
|---|
| 2120 | | $params['conditions'] = ""; |
|---|
| 2121 | | } |
|---|
| 2122 | | } |
|---|
| 2123 | | if($params['conditions']){ |
|---|
| 2124 | | $params['conditions'] = " WHERE ".$params['conditions']; |
|---|
| 2125 | | } |
|---|
| 2126 | | $sql = "UPDATE $table SET $values {$params['conditions']}"; |
|---|
| 2127 | | |
|---|
| 2128 | | $limit_args = array($sql); |
|---|
| 2129 | | if(isset($params['limit'])) { |
|---|
| 2130 | | array_push($limit_args, "limit: $params[limit]"); |
|---|
| 2131 | | } |
|---|
| 2132 | | if(isset($params['offset'])) { |
|---|
| 2133 | | array_push($limit_args, "offset: $params[offset]"); |
|---|
| 2134 | | } |
|---|
| 2135 | | if(count($limit_args)>1) { |
|---|
| 2136 | | $sql = call_user_func_array(array($this,'limit'), $limit_args); |
|---|
| 2137 | | } |
|---|
| 2138 | | |
|---|
| 2139 | | $environment = Config::read("environment.ini"); |
|---|
| 2140 | | $config = $environment->{$this->get_mode()}; |
|---|
| 2141 | | if(!isset($config->database->pdo)||!$config->database->pdo){ |
|---|
| 2142 | | if($config->database->type=="informix"){ |
|---|
| 2143 | | $this->db->set_return_rows(false); |
|---|
| 2144 | | } |
|---|
| 2145 | | } |
|---|
| 2146 | | return $this->db->query($sql); |
|---|
| 2147 | | } |
|---|
| 2148 | | |
|---|
| 2149 | | /** |
|---|
| 2150 | | * Delete All data from Relational Map Table |
|---|
| 2151 | | * |
|---|
| 2152 | | * @param string $conditions |
|---|
| 2153 | | * @return boolean |
|---|
| 2154 | | */ |
|---|
| 2155 | | function delete_all($conditions=''){ |
|---|
| 2156 | | $this->_connect(); |
|---|
| 2157 | | $limit = ""; |
|---|
| 2158 | | if($this->schema){ |
|---|
| 2159 | | $table = $this->schema.".".$this->source; |
|---|
| 2160 | | } else { |
|---|
| 2161 | | $table = $this->source; |
|---|
| 2162 | | } |
|---|
| 2163 | | if(func_num_args()>1){ |
|---|
| 2164 | | $params = get_params(func_get_args()); |
|---|
| 2165 | | |
|---|
| 2166 | | $limit_args = array($select); |
|---|
| 2167 | | if(isset($params['limit'])) { |
|---|
| 2168 | | array_push($limit_args, "limit: $params[limit]"); |
|---|
| 2169 | | } |
|---|
| 2170 | | if(isset($params['offset'])) { |
|---|
| 2171 | | array_push($limit_args, "offset: $params[offset]"); |
|---|
| 2172 | | } |
|---|
| 2173 | | if(count($limit_args)>1) { |
|---|
| 2174 | | $select = call_user_func_array(array($this,'limit'), $limit_args); |
|---|
| 2175 | | } |
|---|
| 2176 | | } |
|---|
| 2177 | | return $this->db->delete($table, $conditions); |
|---|
| 2178 | | } |
|---|
| 2179 | | |
|---|
| 2180 | | /** |
|---|
| 2181 | | * ********************************************************************************* |
|---|
| 2182 | | * Metodos de Debug |
|---|
| 2183 | | * ********************************************************************************* |
|---|
| 2184 | | */ |
|---|
| 2185 | | |
|---|
| 2186 | | /** |
|---|
| 2187 | | * Imprime una version humana de los valores de los campos |
|---|
| 2188 | | * del modelo en una sola linea |
|---|
| 2189 | | * |
|---|
| 2190 | | */ |
|---|
| 2191 | | public function inspect(){ |
|---|
| 2192 | | $this->_connect(); |
|---|
| 2193 | | $inspect = array(); |
|---|
| 2194 | | foreach($this->fields as $field){ |
|---|
| 2195 | | if(!is_array($field)){ |
|---|
| 2196 | | $inspect[] = "$field: {$this->$field}"; |
|---|
| 2197 | | } |
|---|
| 2198 | | } |
|---|
| 2199 | | return join(", ", $inspect); |
|---|
| 2200 | | } |
|---|
| 2201 | | |
|---|
| 2202 | | /** |
|---|
| 2203 | | * ********************************************************************************* |
|---|
| 2204 | | * Metodos de Validacion |
|---|
| 2205 | | * ********************************************************************************* |
|---|
| 2206 | | */ |
|---|
| 2207 | | |
|---|
| 2208 | | /** |
|---|
| 2209 | | * Validate that Attributes cannot have a NULL value |
|---|
| 2210 | | */ |
|---|
| 2211 | | protected function validates_presence_of(){ |
|---|
| 2212 | | $this->_connect(); |
|---|
| 2213 | | |
|---|
| 2214 | | $params = get_params(func_get_args()); |
|---|
| 2215 | | if(is_array($params[0])) { |
|---|
| 2216 | | $params = $params[0]; |
|---|
| 2217 | | } |
|---|
| 2218 | | |
|---|
| 2219 | | $fields = array(); |
|---|
| 2220 | | for($i=0; isset($params[$i]); $i++) { |
|---|
| 2221 | | $fields[] = $params[$i]; |
|---|
| 2222 | | unset($params[$i]); |
|---|
| 2223 | | $i++; |
|---|
| 2224 | | } |
|---|
| 2225 | | |
|---|
| 2226 | | foreach($fields as $p){ |
|---|
| 2227 | | if(!in_array($p, $this->fields)&&!isset($this->$p)){ |
|---|
| 2228 | | throw new ActiveRecordException('No se puede validar presencia de "'.$p.'" |
|---|
| 2229 | | en el modelo '.$this->source.' porque no existe el atributo</u><br> |
|---|
| | 579 | } else { |
|---|
| | 580 | return array(); |
|---|
| | 581 | } |
|---|
| | 582 | } |
|---|
| | 583 | } |
|---|
| | 584 | try { |
|---|
| | 585 | if (method_exists($this, $method)) { |
|---|
| | 586 | call_user_func_array(array($this, $method), $args); |
|---|
| | 587 | } else { |
|---|
| | 588 | if ($has_relation) { |
|---|
| | 589 | throw new ActiveRecordException("No existe el modelo '$model' para relacionar con ActiveRecord::{$this->source}"); |
|---|
| | 590 | } else { |
|---|
| | 591 | throw new ActiveRecordException("No existe el método '$method' en ActiveRecord::" . get_class($this)); |
|---|
| | 592 | } |
|---|
| | 593 | } |
|---|
| | 594 | } |
|---|
| | 595 | catch(Exception $e) { |
|---|
| | 596 | $this->exceptions($e); |
|---|
| | 597 | } |
|---|
| | 598 | return $this->$method($args); |
|---|
| | 599 | } |
|---|
| | 600 | /** |
|---|
| | 601 | * Se conecta a la base de datos y descarga los meta-datos si es necesario |
|---|
| | 602 | * |
|---|
| | 603 | * @param boolean $new_connection |
|---|
| | 604 | */ |
|---|
| | 605 | private function _connect($new_connection = false) { |
|---|
| | 606 | if (!is_object($this->db) || $new_connection) { |
|---|
| | 607 | if ($this->mode) { |
|---|
| | 608 | $this->db = DbBase::raw_connect($new_connection, "mode: {$this->mode}"); |
|---|
| | 609 | } else { |
|---|
| | 610 | $this->db = DbBase::raw_connect($new_connection); |
|---|
| | 611 | } |
|---|
| | 612 | } |
|---|
| | 613 | $this->db->debug = $this->debug; |
|---|
| | 614 | $this->db->logger = $this->logger; |
|---|
| | 615 | $this->dump(); |
|---|
| | 616 | } |
|---|
| | 617 | /** |
|---|
| | 618 | * Cargar los metadatos de la tabla |
|---|
| | 619 | * |
|---|
| | 620 | */ |
|---|
| | 621 | public function dump_model() { |
|---|
| | 622 | $this->_connect(); |
|---|
| | 623 | } |
|---|
| | 624 | /** |
|---|
| | 625 | * Verifica si la tabla definida en $this->source existe |
|---|
| | 626 | * en la base de datos y la vuelca en dump_info |
|---|
| | 627 | * |
|---|
| | 628 | * @return boolean |
|---|
| | 629 | */ |
|---|
| | 630 | protected function dump() { |
|---|
| | 631 | if ($this->_dumped) { |
|---|
| | 632 | return false; |
|---|
| | 633 | } |
|---|
| | 634 | if ($this->source) { |
|---|
| | 635 | $this->source = str_replace(";", "", strtolower($this->source)); |
|---|
| | 636 | } else { |
|---|
| | 637 | $this->_model_name(); |
|---|
| | 638 | if (!$this->source) { |
|---|
| | 639 | return false; |
|---|
| | 640 | } |
|---|
| | 641 | } |
|---|
| | 642 | $table = $this->source; |
|---|
| | 643 | $schema = $this->schema; |
|---|
| | 644 | if (!count(ActiveRecord::get_meta_data($this->source))) { |
|---|
| | 645 | $this->_dumped = true; |
|---|
| | 646 | if ($this->db->table_exists($table, $schema)) { |
|---|
| | 647 | $this->_dump_info($table, $schema); |
|---|
| | 648 | } else { |
|---|
| | 649 | throw new ActiveRecordException("No existe la tabla '$table' en la base de datos"); |
|---|
| | 650 | return false; |
|---|
| | 651 | } |
|---|
| | 652 | if (!count($this->primary_key)) { |
|---|
| | 653 | if (!$this->is_view) { |
|---|
| | 654 | throw new ActiveRecordException("No se ha definido una llave primaria para la tabla '$table' esto imposibilita crear el ActiveRecord para esta entidad"); |
|---|
| | 655 | return false; |
|---|
| | 656 | } |
|---|
| | 657 | } |
|---|
| | 658 | } else { |
|---|
| | 659 | if (!$this->is_dumped()) { |
|---|
| | 660 | $this->_dumped = true; |
|---|
| | 661 | $this->_dump_info($table, $schema); |
|---|
| | 662 | } |
|---|
| | 663 | } |
|---|
| | 664 | return true; |
|---|
| | 665 | } |
|---|
| | 666 | /** |
|---|
| | 667 | * Vuelca la información de la tabla $table en la base de datos |
|---|
| | 668 | * para armar los atributos y meta-data del ActiveRecord |
|---|
| | 669 | * |
|---|
| | 670 | * @param string $table |
|---|
| | 671 | * @return boolean |
|---|
| | 672 | */ |
|---|
| | 673 | private function _dump_info($table, $schema = '') { |
|---|
| | 674 | $this->_dump_lock = true; |
|---|
| | 675 | if (!count(ActiveRecord::get_meta_data($table))) { |
|---|
| | 676 | $meta_data = $this->db->describe_table($table, $schema); |
|---|
| | 677 | if ($meta_data) { |
|---|
| | 678 | ActiveRecord::set_meta_data($table, $meta_data); |
|---|
| | 679 | } |
|---|
| | 680 | } |
|---|
| | 681 | foreach(ActiveRecord::get_meta_data($table) as $field) { |
|---|
| | 682 | $this->fields[] = $field['Field']; |
|---|
| | 683 | if ($field['Key'] == 'PRI') { |
|---|
| | 684 | $this->primary_key[] = $field['Field']; |
|---|
| | 685 | } else $this->non_primary[] = $field['Field']; |
|---|
| | 686 | /** |
|---|
| | 687 | * Si se indica que no puede ser nulo, pero se indica un |
|---|
| | 688 | * valor por defecto, entonces no se incluye en la lista, ya que |
|---|
| | 689 | * al colocar un valor por defecto, el campo nunca sera nulo |
|---|
| | 690 | * |
|---|
| | 691 | */ |
|---|
| | 692 | if ($field['Null'] == 'NO' && !(isset($field['Default']) && $field['Default'])) { |
|---|
| | 693 | $this->not_null[] = $field['Field']; |
|---|
| | 694 | } |
|---|
| | 695 | if (isset($field['Default']) && $field['Default']) { |
|---|
| | 696 | $this->_with_default[] = $field['Field']; |
|---|
| | 697 | } |
|---|
| | 698 | if ($field['Type']) { |
|---|
| | 699 | $this->_data_type[$field['Field']] = strtolower($field['Type']); |
|---|
| | 700 | } |
|---|
| | 701 | if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_at') { |
|---|
| | 702 | $this->_at[] = $field['Field']; |
|---|
| | 703 | } |
|---|
| | 704 | if (substr($field['Field'], strlen($field['Field']) - 3, 3) == '_in') { |
|---|
| | 705 | $this->_in[] = $field['Field']; |
|---|
| | 706 | } |
|---|
| | 707 | } |
|---|
| | 708 | $this->attributes_names = $this->fields; |
|---|
| | 709 | $this->_dump_lock = false; |
|---|
| | 710 | return true; |
|---|
| | 711 | } |
|---|
| | 712 | /** |
|---|
| | 713 | * Commit a Transaction |
|---|
| | 714 | * |
|---|
| | 715 | * @return success |
|---|
| | 716 | */ |
|---|
| | 717 | public function commit() { |
|---|
| | 718 | $this->_connect(); |
|---|
| | 719 | return $this->db->commit(); |
|---|
| | 720 | } |
|---|
| | 721 | /** |
|---|
| | 722 | * Rollback a Transaction |
|---|
| | 723 | * |
|---|
| | 724 | * @return success |
|---|
| | 725 | */ |
|---|
| | 726 | public function rollback() { |
|---|
| | 727 | $this->_connect(); |
|---|
| | 728 | return $this->db->rollback(); |
|---|
| | 729 | } |
|---|
| | 730 | /** |
|---|
| | 731 | * Start a transaction in RDBM |
|---|
| | 732 | * |
|---|
| | 733 | * @return success |
|---|
| | 734 | */ |
|---|
| | 735 | public function begin() { |
|---|
| | 736 | $this->_connect(true); |
|---|
| | 737 | return $this->db->begin(); |
|---|
| | 738 | } |
|---|
| | 739 | /** |
|---|
| | 740 | * Find all records in this table using a SQL Statement |
|---|
| | 741 | * |
|---|
| | 742 | * @param string $sqlQuery |
|---|
| | 743 | * @return ActiveRecord Cursor |
|---|
| | 744 | */ |
|---|
| | 745 | public function find_all_by_sql($sqlQuery) { |
|---|
| | 746 | $this->_connect(); |
|---|
| | 747 | $results = array(); |
|---|
| | 748 | foreach($this->db->fetch_all($sqlQuery) as $result) { |
|---|
| | 749 | $results[] = $this->dump_result($result); |
|---|
| | 750 | } |
|---|
| | 751 | return $results; |
|---|
| | 752 | } |
|---|
| | 753 | /** |
|---|
| | 754 | * Find a record in this table using a SQL Statement |
|---|
| | 755 | * |
|---|
| | 756 | * @param string $sqlQuery |
|---|
| | 757 | * @return ActiveRecord Cursor |
|---|
| | 758 | */ |
|---|
| | 759 | public function find_by_sql($sqlQuery) { |
|---|
| | 760 | $this->_connect(); |
|---|
| | 761 | $row = $this->db->fetch_one($sqlQuery); |
|---|
| | 762 | if ($row !== false) { |
|---|
| | 763 | $this->dump_result_self($row); |
|---|
| | 764 | return $this->dump_result($row); |
|---|
| | 765 | } else { |
|---|
| | 766 | return false; |
|---|
| | 767 | } |
|---|
| | 768 | } |
|---|
| | 769 | /** |
|---|
| | 770 | * Execute a SQL Statement directly |
|---|
| | 771 | * |
|---|
| | 772 | * @param string $sqlQuery |
|---|
| | 773 | * @return int affected |
|---|
| | 774 | */ |
|---|
| | 775 | public function sql($sqlQuery) { |
|---|
| | 776 | $this->_connect(); |
|---|
| | 777 | return $this->db->query($sqlQuery); |
|---|
| | 778 | } |
|---|
| | 779 | /** |
|---|
| | 780 | * Return Fist Record |
|---|
| | 781 | * |
|---|
| | 782 | * @param mixed $what |
|---|
| | 783 | * @param boolean $debug |
|---|
| | 784 | * |
|---|
| | 785 | * Recibe los mismos parametros que find |
|---|
| | 786 | * |
|---|
| | 787 | * @return ActiveRecord Cursor |
|---|
| | 788 | */ |
|---|
| | 789 | public function find_first($what = '') { |
|---|
| | 790 | $this->_connect(); |
|---|
| | 791 | $what = get_params(func_get_args()); |
|---|
| | 792 | $select = "SELECT "; |
|---|
| | 793 | if (isset($what['columns'])) { |
|---|
| | 794 | $select.= ActiveRecord::sql_sanizite($what['columns']); |
|---|
| | 795 | } elseif (isset($what['distinct'])) { |
|---|
| | 796 | $select.= 'DISTINCT '; |
|---|
| | 797 | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
|---|
| | 798 | } else { |
|---|
| | 799 | $select.= join(",", $this->fields); |
|---|
| | 800 | } |
|---|
| | 801 | if ($this->schema) { |
|---|
| | 802 | $select.= " FROM {$this->schema}.{$this->source}"; |
|---|
| | 803 | } else { |
|---|
| | 804 | $select.= " FROM {$this->source}"; |
|---|
| | 805 | } |
|---|
| | 806 | $what['limit'] = 1; |
|---|
| | 807 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 808 | $resp = false; |
|---|
| | 809 | try { |
|---|
| | 810 | $result = $this->db->fetch_one($select); |
|---|
| | 811 | if ($result) { |
|---|
| | 812 | $this->dump_result_self($result); |
|---|
| | 813 | $resp = $this->dump_result($result); |
|---|
| | 814 | } |
|---|
| | 815 | } |
|---|
| | 816 | catch(Exception $e) { |
|---|
| | 817 | $this->exceptions($e); |
|---|
| | 818 | } |
|---|
| | 819 | return $resp; |
|---|
| | 820 | } |
|---|
| | 821 | /** |
|---|
| | 822 | * Find data on Relational Map table |
|---|
| | 823 | * |
|---|
| | 824 | * @param string $what |
|---|
| | 825 | * @return ActiveRecord Cursor |
|---|
| | 826 | * |
|---|
| | 827 | * columns: columnas a utilizar |
|---|
| | 828 | * conditions : condiciones de busqueda en WHERE |
|---|
| | 829 | * join: inclusion inner join o outer join |
|---|
| | 830 | * group : campo para grupo en GROUP BY |
|---|
| | 831 | * having : condicion para el grupo |
|---|
| | 832 | * order : campo para criterio de ordenamiento ORDER BY |
|---|
| | 833 | * distinct: campos para hacer select distinct |
|---|
| | 834 | */ |
|---|
| | 835 | public function find($what = '') { |
|---|
| | 836 | $this->_connect(); |
|---|
| | 837 | $what = get_params(func_get_args()); |
|---|
| | 838 | $select = "SELECT "; |
|---|
| | 839 | if (isset($what['columns'])) { |
|---|
| | 840 | $select.= $what['columns'] ? ActiveRecord::sql_sanizite($what['columns']) : join(",", $this->fields); |
|---|
| | 841 | } elseif (isset($what['distinct'])) { |
|---|
| | 842 | $select.= 'DISTINCT '; |
|---|
| | 843 | $select.= $what['distinct'] ? ActiveRecord::sql_sanizite($what['distinct']) : join(",", $this->fields); |
|---|
| | 844 | } else { |
|---|
| | 845 | $select.= join(",", $this->fields); |
|---|
| | 846 | } |
|---|
| | 847 | if ($this->schema) { |
|---|
| | 848 | $select.= " FROM {$this->schema}.{$this->source}"; |
|---|
| | 849 | } else { |
|---|
| | 850 | $select.= " FROM {$this->source}"; |
|---|
| | 851 | } |
|---|
| | 852 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 853 | $results = array(); |
|---|
| | 854 | $all_results = $this->db->in_query($select); |
|---|
| | 855 | foreach($all_results AS $result) { |
|---|
| | 856 | $results[] = $this->dump_result($result); |
|---|
| | 857 | } |
|---|
| | 858 | $this->count = count($results); |
|---|
| | 859 | if (isset($what[0]) && is_numeric($what[0])) { |
|---|
| | 860 | if (!isset($results[0])) { |
|---|
| | 861 | $this->count = 0; |
|---|
| | 862 | return false; |
|---|
| | 863 | } else { |
|---|
| | 864 | $this->dump_result_self($all_results[0]); |
|---|
| | 865 | $this->count = 1; |
|---|
| | 866 | return $results[0]; |
|---|
| | 867 | } |
|---|
| | 868 | } else { |
|---|
| | 869 | $this->count = count($results); |
|---|
| | 870 | return $results; |
|---|
| | 871 | } |
|---|
| | 872 | } |
|---|
| | 873 | /* |
|---|
| | 874 | * Arma una consulta SQL con el parametro $what, as�: |
|---|
| | 875 | * $what = get_params(func_get_args()); |
|---|
| | 876 | * $select = "SELECT * FROM Clientes"; |
|---|
| | 877 | * $select.= $this->convert_params_to_sql($what); |
|---|
| | 878 | * |
|---|
| | 879 | * @param string $what |
|---|
| | 880 | * @return string |
|---|
| | 881 | */ |
|---|
| | 882 | public function convert_params_to_sql($what = '') { |
|---|
| | 883 | $select = ""; |
|---|
| | 884 | if (is_array($what)) { |
|---|
| | 885 | if (!isset($what['conditions'])) { |
|---|
| | 886 | if (!isset($this->primary_key[0]) && (isset($this->id) || $this->is_view)) { |
|---|
| | 887 | $this->primary_key[0] = "id"; |
|---|
| | 888 | } |
|---|
| | 889 | ActiveRecord::sql_item_sanizite($this->primary_key[0]); |
|---|
| | 890 | if (isset($what[0])) { |
|---|
| | 891 | if (is_numeric($what[0])) { |
|---|
| | 892 | $what['conditions'] = "{$this->primary_key[0]} = {$this->db->add_quotes($what[0]) }"; |
|---|
| | 893 | } else { |
|---|
| | 894 | if ($what[0] == '') { |
|---|
| | 895 | $what['conditions'] = "{$this->primary_key[0]} = ''"; |
|---|
| | 896 | } else { |
|---|
| | 897 | $what['conditions'] = $what[0]; |
|---|
| | 898 | } |
|---|
| | 899 | } |
|---|
| | 900 | } |
|---|
| | 901 | } |
|---|
| | 902 | if (isset($what['join']) && $what['join']) { |
|---|
| | 903 | $select.= " {$what['join']}"; |
|---|
| | 904 | } |
|---|
| | 905 | if (isset($what['conditions']) && $what['conditions']) { |
|---|
| | 906 | $select.= " WHERE {$what['conditions']}"; |
|---|
| | 907 | } |
|---|
| | 908 | if (isset($what['group']) && $what['group']) { |
|---|
| | 909 | $select.= " GROUP BY {$what['group']}"; |
|---|
| | 910 | } |
|---|
| | 911 | if (isset($what['having']) && $what['having']) { |
|---|
| | 912 | $select.= " HAVING {$what['having']}"; |
|---|
| | 913 | } |
|---|
| | 914 | if (isset($what['order']) && $what['order']) { |
|---|
| | 915 | ActiveRecord::sql_sanizite($what['order']); |
|---|
| | 916 | $select.= " ORDER BY {$what['order']}"; |
|---|
| | 917 | } |
|---|
| | 918 | $limit_args = array($select); |
|---|
| | 919 | if (isset($what['limit'])) { |
|---|
| | 920 | array_push($limit_args, "limit: $what[limit]"); |
|---|
| | 921 | } |
|---|
| | 922 | if (isset($what['offset'])) { |
|---|
| | 923 | array_push($limit_args, "offset: $what[offset]"); |
|---|
| | 924 | } |
|---|
| | 925 | if (count($limit_args) > 1) { |
|---|
| | 926 | $select = call_user_func_array(array($this, 'limit'), $limit_args); |
|---|
| | 927 | } |
|---|
| | 928 | } else { |
|---|
| | 929 | if (strlen($what)) { |
|---|
| | 930 | if (is_numeric($what)) { |
|---|
| | 931 | $select.= "WHERE {$this->primary_key[0]} = '$what'"; |
|---|
| | 932 | } else { |
|---|
| | 933 | $select.= "WHERE $what"; |
|---|
| | 934 | } |
|---|
| | 935 | } |
|---|
| | 936 | } |
|---|
| | 937 | return $select; |
|---|
| | 938 | } |
|---|
| | 939 | /* |
|---|
| | 940 | * Devuelve una clausula LIMIT adecuada al RDBMS empleado |
|---|
| | 941 | * |
|---|
| | 942 | * limit: maxima cantidad de elementos a mostrar |
|---|
| | 943 | * offset: desde que elemento se comienza a mostrar |
|---|
| | 944 | * |
|---|
| | 945 | * @param string $sql consulta select |
|---|
| | 946 | * @return String clausula LIMIT adecuada al RDBMS empleado |
|---|
| | 947 | */ |
|---|
| | 948 | public function limit($sql) { |
|---|
| | 949 | $args = func_get_args(); |
|---|
| | 950 | return call_user_func_array(array($this->db, 'limit'), $args); |
|---|
| | 951 | } |
|---|
| | 952 | /** |
|---|
| | 953 | * Ejecuta un SELECT DISTINCT |
|---|
| | 954 | * @param string $what |
|---|
| | 955 | * @return array |
|---|
| | 956 | * |
|---|
| | 957 | * Soporta parametros iguales a find |
|---|
| | 958 | * |
|---|
| | 959 | */ |
|---|
| | 960 | public function distinct($what = '') { |
|---|
| | 961 | $this->_connect(); |
|---|
| | 962 | $what = get_params(func_get_args()); |
|---|
| | 963 | if ($this->schema) { |
|---|
| | 964 | $table = $this->schema . "." . $this->source; |
|---|
| | 965 | } else { |
|---|
| | 966 | $table = $this->source; |
|---|
| | 967 | } |
|---|
| | 968 | if (!isset($what['columns'])) { |
|---|
| | 969 | $what['columns'] = $what['0']; |
|---|
| | 970 | } else { |
|---|
| | 971 | if (!$what['columns']) { |
|---|
| | 972 | $what['columns'] = $what['0']; |
|---|
| | 973 | } |
|---|
| | 974 | } |
|---|
| | 975 | $what['columns'] = ActiveRecord::sql_sanizite($what['columns']); |
|---|
| | 976 | $select = "SELECT DISTINCT {$what['columns']} FROM $table "; |
|---|
| | 977 | /** |
|---|
| | 978 | * Se elimina el de indice cero ya que por defecto convert_params_to_sql lo considera como una condicion en WHERE |
|---|
| | 979 | */ |
|---|
| | 980 | unset($what[0]); |
|---|
| | 981 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 982 | $results = array(); |
|---|
| | 983 | foreach($this->db->fetch_all($select) as $result) { |
|---|
| | 984 | $results[] = $result[0]; |
|---|
| | 985 | } |
|---|
| | 986 | return $results; |
|---|
| | 987 | } |
|---|
| | 988 | /** |
|---|
| | 989 | * Ejecuta una consulta en el RDBM directamente |
|---|
| | 990 | * |
|---|
| | 991 | * @param string $sql |
|---|
| | 992 | * @return resource |
|---|
| | 993 | */ |
|---|
| | 994 | public function select_one($sql) { |
|---|
| | 995 | $this->_connect(); |
|---|
| | 996 | if (substr(ltrim($sql), 0, 7) != "SELECT") { |
|---|
| | 997 | $sql = "SELECT " . $sql; |
|---|
| | 998 | } |
|---|
| | 999 | $num = $this->db->fetch_one($sql); |
|---|
| | 1000 | return $num[0]; |
|---|
| | 1001 | } |
|---|
| | 1002 | static public function static_select_one($sql) { |
|---|
| | 1003 | $db = db::raw_connect(); |
|---|
| | 1004 | if (substr(ltrim($sql), 0, 7) != "SELECT") { |
|---|
| | 1005 | $sql = "SELECT " . $sql; |
|---|
| | 1006 | } |
|---|
| | 1007 | $num = $db->fetch_one($sql); |
|---|
| | 1008 | return $num[0]; |
|---|
| | 1009 | } |
|---|
| | 1010 | /** |
|---|
| | 1011 | * Realiza un conteo de filas |
|---|
| | 1012 | * |
|---|
| | 1013 | * @param string $what |
|---|
| | 1014 | * @return integer |
|---|
| | 1015 | */ |
|---|
| | 1016 | public function count($what = '') { |
|---|
| | 1017 | $this->_connect(); |
|---|
| | 1018 | $what = get_params(func_get_args()); |
|---|
| | 1019 | if ($this->schema) { |
|---|
| | 1020 | $table = "{$this->schema}.{$this->source}"; |
|---|
| | 1021 | } else { |
|---|
| | 1022 | $table = $this->source; |
|---|
| | 1023 | } |
|---|
| | 1024 | if (isset($what['distinct']) && $what['distinct']) { |
|---|
| | 1025 | if (isset($what['group']) || isset($what['order'])) { |
|---|
| | 1026 | $select = "SELECT COUNT(*) FROM (SELECT DISTINCT {$what['distinct']} FROM $table "; |
|---|
| | 1027 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1028 | $select.= ') AS t '; |
|---|
| | 1029 | } else { |
|---|
| | 1030 | $select = "SELECT COUNT(DISTINCT {$what['distinct']}) FROM $table "; |
|---|
| | 1031 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1032 | } |
|---|
| | 1033 | } else { |
|---|
| | 1034 | $select = "SELECT COUNT(*) FROM $table "; |
|---|
| | 1035 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1036 | } |
|---|
| | 1037 | $num = $this->db->fetch_one($select); |
|---|
| | 1038 | return $num[0]; |
|---|
| | 1039 | } |
|---|
| | 1040 | /** |
|---|
| | 1041 | * Realiza un promedio sobre el campo $what |
|---|
| | 1042 | * |
|---|
| | 1043 | * @param string $what |
|---|
| | 1044 | * @return array |
|---|
| | 1045 | */ |
|---|
| | 1046 | public function average($what = '') { |
|---|
| | 1047 | $this->_connect(); |
|---|
| | 1048 | $what = get_params(func_get_args()); |
|---|
| | 1049 | if (isset($what['column'])) { |
|---|
| | 1050 | if (!$what['column']) { |
|---|
| | 1051 | $what['column'] = $what[0]; |
|---|
| | 1052 | } |
|---|
| | 1053 | } else { |
|---|
| | 1054 | $what['column'] = $what[0]; |
|---|
| | 1055 | } |
|---|
| | 1056 | unset($what[0]); |
|---|
| | 1057 | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| | 1058 | if ($this->schema) { |
|---|
| | 1059 | $table = "{$this->schema}.{$this->source}"; |
|---|
| | 1060 | } else { |
|---|
| | 1061 | $table = $this->source; |
|---|
| | 1062 | } |
|---|
| | 1063 | $select = "SELECT AVG({$what['column']}) FROM $table "; |
|---|
| | 1064 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1065 | $num = $this->db->fetch_one($select); |
|---|
| | 1066 | return $num[0]; |
|---|
| | 1067 | } |
|---|
| | 1068 | public function sum($what = '') { |
|---|
| | 1069 | $this->_connect(); |
|---|
| | 1070 | $what = get_params(func_get_args()); |
|---|
| | 1071 | if (isset($what['column'])) { |
|---|
| | 1072 | if (!$what['column']) { |
|---|
| | 1073 | $what['column'] = $what[0]; |
|---|
| | 1074 | } |
|---|
| | 1075 | } else { |
|---|
| | 1076 | $what['column'] = $what[0]; |
|---|
| | 1077 | } |
|---|
| | 1078 | unset($what[0]); |
|---|
| | 1079 | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| | 1080 | if ($this->schema) { |
|---|
| | 1081 | $table = "{$this->schema}.{$this->source}"; |
|---|
| | 1082 | } else { |
|---|
| | 1083 | $table = $this->source; |
|---|
| | 1084 | } |
|---|
| | 1085 | $select = "SELECT SUM({$what['column']}) FROM $table "; |
|---|
| | 1086 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1087 | $num = $this->db->fetch_one($select); |
|---|
| | 1088 | return $num[0]; |
|---|
| | 1089 | } |
|---|
| | 1090 | /** |
|---|
| | 1091 | * Busca el valor maximo para el campo $what |
|---|
| | 1092 | * |
|---|
| | 1093 | * @param string $what |
|---|
| | 1094 | * @return mixed |
|---|
| | 1095 | */ |
|---|
| | 1096 | public function maximum($what = '') { |
|---|
| | 1097 | $this->_connect(); |
|---|
| | 1098 | $what = get_params(func_get_args()); |
|---|
| | 1099 | if (isset($what['column'])) { |
|---|
| | 1100 | if (!$what['column']) { |
|---|
| | 1101 | $what['column'] = $what[0]; |
|---|
| | 1102 | } |
|---|
| | 1103 | } else { |
|---|
| | 1104 | $what['column'] = $what[0]; |
|---|
| | 1105 | } |
|---|
| | 1106 | unset($what[0]); |
|---|
| | 1107 | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| | 1108 | if ($this->schema) { |
|---|
| | 1109 | $table = "{$this->schema}.{$this->source}"; |
|---|
| | 1110 | } else { |
|---|
| | 1111 | $table = $this->source; |
|---|
| | 1112 | } |
|---|
| | 1113 | $select = "SELECT MAX({$what['column']}) FROM $table "; |
|---|
| | 1114 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1115 | $num = $this->db->fetch_one($select); |
|---|
| | 1116 | return $num[0]; |
|---|
| | 1117 | } |
|---|
| | 1118 | /** |
|---|
| | 1119 | * Busca el valor minimo para el campo $what |
|---|
| | 1120 | * |
|---|
| | 1121 | * @param string $what |
|---|
| | 1122 | * @return mixed |
|---|
| | 1123 | */ |
|---|
| | 1124 | public function minimum($what = '') { |
|---|
| | 1125 | $this->_connect(); |
|---|
| | 1126 | $what = get_params(func_get_args()); |
|---|
| | 1127 | if (isset($what['column'])) { |
|---|
| | 1128 | if (!$what['column']) { |
|---|
| | 1129 | $what['column'] = $what[0]; |
|---|
| | 1130 | } |
|---|
| | 1131 | } else { |
|---|
| | 1132 | $what['column'] = $what[0]; |
|---|
| | 1133 | } |
|---|
| | 1134 | unset($what[0]); |
|---|
| | 1135 | ActiveRecord::sql_item_sanizite($what['column']); |
|---|
| | 1136 | if ($this->schema) { |
|---|
| | 1137 | $table = "{$this->schema}.{$this->source}"; |
|---|
| | 1138 | } else { |
|---|
| | 1139 | $table = $this->source; |
|---|
| | 1140 | } |
|---|
| | 1141 | $select = "SELECT MIN({$what['column']}) FROM $table "; |
|---|
| | 1142 | $select.= $this->convert_params_to_sql($what); |
|---|
| | 1143 | $num = $this->db->fetch_one($select); |
|---|
| | 1144 | return $num[0]; |
|---|
| | 1145 | } |
|---|
| | 1146 | /** |
|---|
| | 1147 | * Realiza un conteo directo mediante $sql |
|---|
| | 1148 | * |
|---|
| | 1149 | * @param string $sqlQuery |
|---|
| | 1150 | * @return mixed |
|---|
| | 1151 | */ |
|---|
| | 1152 | public function count_by_sql($sqlQuery) { |
|---|
| | 1153 | $this->_connect(); |
|---|
| | 1154 | $num = $this->db->fetch_one($sqlQuery); |
|---|
| | 1155 | return $num[0]; |
|---|
| | 1156 | } |
|---|
| | 1157 | /** |
|---|
| | 1158 | * Iguala los valores de un resultado de la base de datos |
|---|
| | 1159 | * en un nuevo objeto con sus correspondientes |
|---|
| | 1160 | * atributos de la clase |
|---|
| | 1161 | * |
|---|
| | 1162 | * @param array $result |
|---|
| | 1163 | * @return ActiveRecord |
|---|
| | 1164 | */ |
|---|
| | 1165 | function dump_result($result) { |
|---|
| | 1166 | $this->_connect(); |
|---|
| | 1167 | $obj = clone $this; |
|---|
| | 1168 | /** |
|---|
| | 1169 | * Consulta si la clase es padre de otra y crea el tipo de dato correcto |
|---|
| | 1170 | */ |
|---|
| | 1171 | if (isset($result['type'])) { |
|---|
| | 1172 | if (in_array($result['type'], $this->parent_of)) { |
|---|
| | 1173 | if (class_exists($result['type'])) { |
|---|
| | 1174 | $obj = new $result['type']; |
|---|
| | 1175 | unset($result['type']); |
|---|
| | 1176 | } |
|---|
| | 1177 | } |
|---|
| | 1178 | } |
|---|
| | 1179 | $this->_dump_lock = true; |
|---|
| | 1180 | if (is_array($result)) { |
|---|
| | 1181 | foreach($result as $k => $r) { |
|---|
| | 1182 | if (!is_numeric($k)) { |
|---|
| | 1183 | $obj->$k = stripslashes($r); |
|---|
| | 1184 | } |
|---|
| | 1185 | } |
|---|
| | 1186 | } |
|---|
| | 1187 | $this->_dump_lock = false; |
|---|
| | 1188 | return $obj; |
|---|
| | 1189 | } |
|---|
| | 1190 | /** |
|---|
| | 1191 | * Iguala los valores de un resultado de la base de datos |
|---|
| | 1192 | * con sus correspondientes atributos de la clase |
|---|
| | 1193 | * |
|---|
| | 1194 | * @param array $result |
|---|
| | 1195 | * @return ActiveRecord |
|---|
| | 1196 | */ |
|---|
| | 1197 | public function dump_result_self($result) { |
|---|
| | 1198 | $this->_connect(); |
|---|
| | 1199 | $this->_dump_lock = true; |
|---|
| | 1200 | if (is_array($result)) { |
|---|
| | 1201 | foreach($result as $k => $r) { |
|---|
| | 1202 | if (!is_numeric($k)) { |
|---|
| | 1203 | $this->$k = stripslashes($r); |
|---|
| | 1204 | } |
|---|
| | 1205 | } |
|---|
| | 1206 | } |
|---|
| | 1207 | $this->_dump_lock = false; |
|---|
| | 1208 | } |
|---|
| | 1209 | /** |
|---|
| | 1210 | * Create a new Row using values from $_REQUEST |
|---|
| | 1211 | * |
|---|
| | 1212 | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
|---|
| | 1213 | * @return boolean success |
|---|
| | 1214 | */ |
|---|
| | 1215 | public function create_from_request($form = '') { |
|---|
| | 1216 | if ($form) { |
|---|
| | 1217 | return $this->create($_REQUEST[$form]); |
|---|
| | 1218 | } else { |
|---|
| | 1219 | return $this->create($_REQUEST); |
|---|
| | 1220 | } |
|---|
| | 1221 | } |
|---|
| | 1222 | /** |
|---|
| | 1223 | * Saves a new Row using values from $_REQUEST |
|---|
| | 1224 | * |
|---|
| | 1225 | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
|---|
| | 1226 | * @return boolean success |
|---|
| | 1227 | */ |
|---|
| | 1228 | public function save_from_request($form = '') { |
|---|
| | 1229 | if ($form) { |
|---|
| | 1230 | return $this->save($_REQUEST[$form]); |
|---|
| | 1231 | } else { |
|---|
| | 1232 | return $this->save($_REQUEST); |
|---|
| | 1233 | } |
|---|
| | 1234 | } |
|---|
| | 1235 | /** |
|---|
| | 1236 | * Updates a Row using values from $_REQUEST |
|---|
| | 1237 | * |
|---|
| | 1238 | * @param string $form form name for request, equivalent to $_REQUEST[$form] |
|---|
| | 1239 | * @return boolean success |
|---|
| | 1240 | */ |
|---|
| | 1241 | public function update_from_request($form = '') { |
|---|
| | 1242 | if ($form) { |
|---|
| | 1243 | return $this->update($_REQUEST[$form]); |
|---|
| | 1244 | } else { |
|---|
| | 1245 | return $this->update($_REQUEST); |
|---|
| | 1246 | } |
|---|
| | 1247 | } |
|---|
| | 1248 | /** |
|---|
| | 1249 | * Creates a new Row in map table |
|---|
| | 1250 | * |
|---|
| | 1251 | * @param mixed $values |
|---|
| | 1252 | * @return success boolean |
|---|
| | 1253 | */ |
|---|
| | 1254 | public function create() { |
|---|
| | 1255 | $this->_connect(); |
|---|
| | 1256 | if (func_num_args() > 0) { |
|---|
| | 1257 | $params = get_params(func_get_args()); |
|---|
| | 1258 | $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params; |
|---|
| | 1259 | foreach($this->fields as $field) { |
|---|
| | 1260 | if (isset($values[$field])) { |
|---|
| | 1261 | $this->$field = $values[$field]; |
|---|
| | 1262 | } |
|---|
| | 1263 | } |
|---|
| | 1264 | } |
|---|
| | 1265 | if ($this->primary_key[0] == 'id') { |
|---|
| | 1266 | $this->id = null; |
|---|
| | 1267 | } |
|---|
| | 1268 | return $this->save(); |
|---|
| | 1269 | } |
|---|
| | 1270 | /** |
|---|
| | 1271 | * Consulta si un determinado registro existe o no |
|---|
| | 1272 | * en la entidad de la base de datos |
|---|
| | 1273 | * |
|---|
| | 1274 | * @return boolean |
|---|
| | 1275 | */ |
|---|
| | 1276 | function exists($where_pk = '') { |
|---|
| | 1277 | $this->_connect(); |
|---|
| | 1278 | if ($this->schema) { |
|---|
| | 1279 | $table = "{$this->schema}.{$this->source}"; |
|---|
| | 1280 | } else { |
|---|
| | 1281 | $table = $this->source; |
|---|
| | 1282 | } |
|---|
| | 1283 | if (!$where_pk) { |
|---|
| | 1284 | $where_pk = array(); |
|---|
| | 1285 | foreach($this->primary_key as $key) { |
|---|
| | 1286 | if ($this->$key) { |
|---|
| | 1287 | $where_pk[] = " $key = '{$this->$key}'"; |
|---|
| | 1288 | } |
|---|
| | 1289 | } |
|---|
| | 1290 | if (count($where_pk)) { |
|---|
| | 1291 | $this->_where_pk = join(" AND ", $where_pk); |
|---|
| | 1292 | } else { |
|---|
| | 1293 | return 0; |
|---|
| | 1294 | } |
|---|
| | 1295 | $query = "SELECT COUNT(*) FROM $table WHERE {$this->_where_pk}"; |
|---|
| | 1296 | } else { |
|---|
| | 1297 | if (is_numeric($where_pk)) { |
|---|
| | 1298 | $query = "SELECT(*) FROM $table WHERE id = '$where_pk'"; |
|---|
| | 1299 | } else { |
|---|
| | 1300 | $query = "SELECT COUNT(*) FROM $table WHERE $where_pk"; |
|---|
| | 1301 | } |
|---|
| | 1302 | } |
|---|
| | 1303 | $num = $this->db->fetch_one($query); |
|---|
| | 1304 | return $num[0]; |
|---|
| | 1305 | } |
|---|
| | 1306 | /** |
|---|
| | 1307 | * Saves Information on the ActiveRecord Properties |
|---|
| | 1308 | * @param array $values array de valores a cargar |
|---|
| | 1309 | * @return boolean success |
|---|
| | 1310 | */ |
|---|
| | 1311 | public function save() { |
|---|
| | 1312 | $this->_connect(); |
|---|
| | 1313 | if (func_num_args() > 0) { |
|---|
| | 1314 | $params = get_params(func_get_args()); |
|---|
| | 1315 | $values = (isset($params[0]) && is_array($params[0])) ? $params[0] : $params; |
|---|
| | 1316 | foreach($this->fields as $field) { |
|---|
| | 1317 | if (isset($values[$field])) { |
|---|
| | 1318 | $this->$field = $values[$field]; |
|---|
| | 1319 | } |
|---|
| | 1320 | } |
|---|
| | 1321 | } |
|---|
| | 1322 | $ex = $this->exists(); |
|---|
| | 1323 | if ($this->schema) { |
|---|
| | 1324 | $table = $this->schema . "." . $this->source; |
|---|
| | 1325 | } else { |
|---|
| | 1326 | $table = $this->source; |
|---|
| | 1327 | } |
|---|
| | 1328 | #Run Validation Callbacks Before |
|---|
| | 1329 | if (method_exists($this, 'before_validation')) { |
|---|
| | 1330 | if ($this->before_validation() == 'cancel') { |
|---|
| | 1331 | return false; |
|---|
| | 1332 | } |
|---|
| | 1333 | } else { |
|---|
| | 1334 | if (isset($this->before_validation)) { |
|---|
| | 1335 | $method = $this->before_validation; |
|---|
| | 1336 | if ($this->$method() == 'cancel') { |
|---|
| | 1337 | return false; |
|---|
| | 1338 | } |
|---|
| | 1339 | } |
|---|
| | 1340 | } |
|---|
| | 1341 | if (!$ex && method_exists($this, "before_validation_on_create")) { |
|---|
| | 1342 | if ($this->before_validation_on_create() == 'cancel') { |
|---|
| | 1343 | return false; |
|---|
| | 1344 | } |
|---|
| | 1345 | } else { |
|---|
| | 1346 | if (isset($this->before_validation_on_create)) { |
|---|
| | 1347 | $method = $this->before_validation_on_create; |
|---|
| | 1348 | if ($this->$method() == 'cancel') { |
|---|
| | 1349 | return false; |
|---|
| | 1350 | } |
|---|
| | 1351 | } |
|---|
| | 1352 | } |
|---|
| | 1353 | if ($ex && method_exists($this, "before_validation_on_update")) { |
|---|
| | 1354 | if ($this->before_validation_on_update() == 'cancel') { |
|---|
| | 1355 | return false; |
|---|
| | 1356 | } |
|---|
| | 1357 | } else { |
|---|
| | 1358 | if (isset($this->before_validation_on_update)) { |
|---|
| | 1359 | $method = $this->before_validation_on_update; |
|---|
| | 1360 | if ($this->$method() == 'cancel') { |
|---|
| | 1361 | return false; |
|---|
| | 1362 | } |
|---|
| | 1363 | } |
|---|
| | 1364 | } |
|---|
| | 1365 | /** |
|---|
| | 1366 | * Recordamos que aqui no aparecen los que tienen valores por defecto, |
|---|
| | 1367 | * pero sin embargo se debe estar pendiente de validar en las otras verificaciones |
|---|
| | 1368 | * los campos nulos, ya que en estas si el campo es nulo, realmente se refiere a un campo que |
|---|
| | 1369 | * debe tomar el valor por defecto |
|---|
| | 1370 | * |
|---|
| | 1371 | */ |
|---|
| | 1372 | $e = false; |
|---|
| | 1373 | for ($i = 0;$i <= count($this->not_null) - 1;$i++) { |
|---|
| | 1374 | $f = $this->not_null[$i]; |
|---|
| | 1375 | if (isset($this->$f) && (is_null($this->$f) || $this->$f == '')) { |
|---|
| | 1376 | if (!$ex && $f == 'id') { |
|---|
| | 1377 | continue; |
|---|
| | 1378 | } |
|---|
| | 1379 | if (!$ex && in_array($f, $this->_at)) { |
|---|
| | 1380 | continue; |
|---|
| | 1381 | } |
|---|
| | 1382 | if ($ex && in_array($f, $this->_in)) { |
|---|
| | 1383 | continue; |
|---|
| | 1384 | } |
|---|
| | 1385 | Flash::error("Error: El campo $f no puede ser nulo"); |
|---|
| | 1386 | $e = true; |
|---|
| | 1387 | } |
|---|
| | 1388 | } |
|---|
| | 1389 | if ($e) { |
|---|
| | 1390 | return false; |
|---|
| | 1391 | } |
|---|
| | 1392 | /** |
|---|
| | 1393 | * Validacion validates_presence |
|---|
| | 1394 | * |
|---|
| | 1395 | */ |
|---|
| | 1396 | $e = false; |
|---|
| | 1397 | foreach($this->_validates_presence as $f => $opt) { |
|---|
| | 1398 | $f = $this->not_null[$i]; |
|---|
| | 1399 | if (isset($this->$f) && (is_null($this->$f) || $this->$f == '')) { |
|---|
| | 1400 | if (!$ex && $f == 'id') { |
|---|
| | 1401 | continue; |
|---|
| | 1402 | } |
|---|
| | 1403 | if (!$ex && in_array($f, $this->_at)) { |
|---|
| | 1404 | continue; |
|---|
| | 1405 | } |
|---|
| | 1406 | if ($ex && in_array($f, $this->_in)) { |
|---|
| | 1407 | continue; |
|---|
| | 1408 | } |
|---|
| | 1409 | if (isset($opt['message'])) { |
|---|
| | 1410 | Flash::error($opt['message']); |
|---|
| | 1411 | } else { |
|---|
| | 1412 | $field = isset($opt['field']) ? $opt['field'] : $f; |
|---|
| | 1413 | Flash::error("Error: El campo $field no puede ser nulo"); |
|---|
| | 1414 | } |
|---|
| | 1415 | $e = true; |
|---|
| | 1416 | } |
|---|
| | 1417 | } |
|---|
| | 1418 | if ($e) { |
|---|
| | 1419 | return false; |
|---|
| | 1420 | } |
|---|
| | 1421 | /** |
|---|
| | 1422 | * Validacion validates_length |
|---|
| | 1423 | * |
|---|
| | 1424 | */ |
|---|
| | 1425 | $e = false; |
|---|
| | 1426 | foreach($this->_validates_length as $f => $opt) { |
|---|
| | 1427 | if (isset($this->$f) && !is_null($this->$f) && $this->$f != '') { |
|---|
| | 1428 | $field = isset($opt['field']) ? $opt['field'] : $f; |
|---|
| | 1429 | if (isset($opt['in'])) { |
|---|
| | 1430 | $in = explode(":", $opt['in']); |
|---|
| | 1431 | if (is_numeric($in[0]) && is_numeric($in[1])) { |
|---|
| | 1432 | $opt['minimum'] = $in[0]; |
|---|
| | 1433 | $opt['maximum'] = $in[1]; |
|---|
| | 1434 | } |
|---|
| | 1435 | } |
|---|
| | 1436 | if (isset($opt['minimum']) && is_numeric($opt['minimum'])) { |
|---|
| | 1437 | $n = $opt['minimum']; |
|---|
| | 1438 | if (strlen($this->$f) < $n) { |
|---|
| | 1439 | if (!isset($opt['too_short'])) { |
|---|
| | 1440 | Flash::error("Error: El campo $field debe tener como mínimo $n caracteres"); |
|---|
| | 1441 | $e = true; |
|---|
| | 1442 | } else { |
|---|
| | 1443 | Flash::error($opt['too_short']); |
|---|
| | 1444 | $e = true; |
|---|
| | 1445 | } |
|---|
| | 1446 | } |
|---|
| | 1447 | } |
|---|
| | 1448 | if (isset($opt['maximum']) && is_numeric($opt['maximum'])) { |
|---|
| | 1449 | $n = $opt['maximum']; |
|---|
| | 1450 | if (strlen($this->$f) > $n) { |
|---|
| | 1451 | if (!isset($opt['too_long'])) { |
|---|
| | 1452 | Flash::error("Error: El campo $field debe tener como máximo $n caracteres"); |
|---|
| | 1453 | $e = true; |
|---|
| | 1454 | } else { |
|---|
| | 1455 | Flash::error($opt['too_long']); |
|---|
| | 1456 | $e = true; |
|---|
| | 1457 | } |
|---|
| | 1458 | } |
|---|
| | 1459 | } |
|---|
| | 1460 | } |
|---|
| | 1461 | } |
|---|
| | 1462 | if ($e) { |
|---|
| | 1463 | return false; |
|---|
| | 1464 | } |
|---|
| | 1465 | /** |
|---|
| | 1466 | * Validacion validates_inclusion |
|---|
| | 1467 | * |
|---|
| | 1468 | */ |
|---|
| | 1469 | $e = false; |
|---|
| | 1470 | foreach($this->_validates_inclusion as $finc => $list) { |
|---|
| | 1471 | if (isset($this->$finc) && !is_null($this->$finc) && $this->$finc != '') { |
|---|
| | 1472 | if (!is_array($list[1]) && $this->$finc != $list[1]) { |
|---|
| | 1473 | if (isset($list['message'])) { |
|---|
| | 1474 | Flash::error($list['message']); |
|---|
| | 1475 | } else { |
|---|
| | 1476 | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
|---|
| | 1477 | Flash::error("$field debe tener un valor entre ({$list[1]})"); |
|---|
| | 1478 | } |
|---|
| | 1479 | $e = true; |
|---|
| | 1480 | } else { |
|---|
| | 1481 | if (!in_array($this->$finc, $list[1])) { |
|---|
| | 1482 | if (isset($list['message'])) { |
|---|
| | 1483 | Flash::error($list['message']); |
|---|
| | 1484 | } else { |
|---|
| | 1485 | $field = isset($list['field']) ? $list['field'] : ucwords($finc); |
|---|
| | 1486 | Flash::error("$field debe tener un valor entre (" . join(",", $list[1]) . ")"); |
|---|
| | 1487 | } |
|---|
| | 1488 | $e = true; |
|---|
| | 1489 | } |
|---|
| | 1490 | } |
|---|
| | 1491 | } |
|---|
| | 1492 | } |
|---|
| | 1493 | if ($e) { |
|---|
| | 1494 | return false; |
|---|
| | 1495 | } |
|---|
| | 1496 | /** |
|---|
| | 1497 | * Validacion validates_exclusion |
|---|
| | 1498 | * |
|---|
| | 1499 | */ |
|---|
| | 1500 | $e = false; |
|---|
| | 1501 | foreach($this->_validates_exclusion as $finc => $list) { |
|---|
| | 1502 | if (isset($this->$finc) && !is_null($this->$finc) && $this->$finc != '') { |
|---|
| | 1503 | if (!is_array($list[1]) && $this->$finc == $list[1]) { |
|---|
| | 1504 | if (isset($list['message'])) { |
|---|
| | 1505 | Flash::error($list['message']); |
|---|
| | 1506 | } else { |
|---|
| | 1507 | $field = isset($list['field']) ? $list['field'] : ucwords($finc);&nb |
|---|