Skip to content

fix: ajustar el manejo de valores por defecto para campos de fecha y datetime#7

Closed
daniel89fg wants to merge 1 commit intomasterfrom
bug-columna-datetime
Closed

fix: ajustar el manejo de valores por defecto para campos de fecha y datetime#7
daniel89fg wants to merge 1 commit intomasterfrom
bug-columna-datetime

Conversation

@daniel89fg
Copy link
Owner

Cuando se añade una columna de tipo fecha y esa columna es not null, si no se rellena el clear del modelo puede rellenarse con valores del 01-01-1970 de forma automática, de este modo corregimos ese problema desde el core, asumiendo que las columnas de tipo fecha no tienen un valor por defecto.

¿Cómo has probado los cambios?

Toda modificación debe haber sido mínimamente probada. Marca o describe las pruebas que has realizado:

  • He revisado mi código antes de enviarlo.
  • He probado que funciona correctamente en mi PC.
  • He probado que funciona correctamente con una base de datos vacía.
  • He ejecutado los tests unitarios.

How has the changes been tested? (english)

Any modification must have been tested minimally. Mark or describe the tests you have performed:

  • I have reviewed my code before sending it.
  • I have tested it on my PC.
  • I have tested it on an empty database.
  • I have run the unit tests.

Ayuda

Aquí tienes algunos enlaces de ayuda:

Help (english)

Here are some help links:

Copilot AI review requested due to automatic review settings November 21, 2025 08:00
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses an issue with default value handling for date and datetime fields in the model's clear() method. When adding a NOT NULL date column without providing a default value, the system was previously auto-filling with values from 01-01-1970 (Unix epoch). The fix assumes that date and datetime columns should never have default values and always calls Tools::date() and Tools::dateTime() without parameters to set them to the current date/time.

Key changes:

  • Modified the clear() method in ModelClass.php to ignore the $field['default'] parameter for date and datetime fields
  • Both date and datetime fields now always use the current date/time when clearing a model

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +200 to +201
'date' => Tools::date(), // asumimos que el campo fecha nunca tendrá valor por defecto
'datetime', 'timestamp' => Tools::dateTime(), // asumimos que el campo datetime nunca tendrá valor por defecto
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assumption that date/datetime fields never have default values may not hold true for all use cases. Some fields might legitimately need a specific default date (e.g., a system start date, or a far-future date for 'never expires' scenarios). This change breaks backward compatibility if any models rely on database-defined default values for date fields. Consider checking if $field['default'] is empty/null before falling back to the current date, allowing models with explicitly defined defaults to continue working: 'date' => empty($field['default']) ? Tools::date() : Tools::date($field['default'])

Suggested change
'date' => Tools::date(), // asumimos que el campo fecha nunca tendrá valor por defecto
'datetime', 'timestamp' => Tools::dateTime(), // asumimos que el campo datetime nunca tendrá valor por defecto
'date' => empty($field['default']) ? Tools::date() : Tools::date($field['default']),
'datetime', 'timestamp' => empty($field['default']) ? Tools::dateTime() : Tools::dateTime($field['default']),

Copilot uses AI. Check for mistakes.
@daniel89fg daniel89fg closed this Nov 26, 2025
@daniel89fg daniel89fg deleted the bug-columna-datetime branch November 28, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants