Hi All,
I am facing a problem with Migx where I have on my MySQL Schema some dates with datetime format, like this:
<field key="start_date" dbtype="datetime" phptype="datetime" null="true" />
<field key="end_date" dbtype="datetime" phptype="datetime" null="true" />
And if I don’t set them, the value shown for 0000-00-00 00:00:00 will be 1970-01-01 00:00:00.
This is specially tricky because on the database it is added 1970-01-01 00:00:00 and filtering all this it is a bit of an headache.
I’m wondering if using unix time format would solve this problem or if you were able to sort this out with other ways? I am on the critical phase of defining now these settings. Later will be too late
Thanks in advance for your help.
markh
August 19, 2019, 9:03am
2
What do you want the value to be if you don’t set it?
Hi Mark,
Should be 0000-00-00 00:00:00.
One of the thinks I thought it could work was to have this field in the database as varchar instead of date. The empty value would be stored as empty or as 0000-00-00 00:00:00 but I know it is not the best solution.
markh
August 19, 2019, 11:09am
4
Try:
<field key="start_date" dbtype="datetime" phptype="datetime" null="true" default="0000-00-00 00:00:00" />
Note that MySQL Strict mode doesn’t support the zero value for a datetime field.
It’s also worth checking what your current model does. Does it actually write 1970-01-01 00:00:00
to the database, or is a literal NULL
value stored and that is only rendered as such? In the latter case, it may be something you can tweak in the rendering, but I don’t know much about MIGX.
I do personally tend to use unix timestamps because they’re less… magical .
Well, when trying to change these fields, this is what I get:
[2019-08-19 13:24:13] (ERROR in xPDOManager_mysql::alterField @ D:\xampp\htdocs\core\xpdo\om\mysql\xpdomanager.class.php : 363) Error altering field InscricoesCartasDates->end_date: Array
(
[0] => 42000
[1] => 1067
[2] => Invalid default value for 'start_date'
)
By checking the database for saved values, it is saves as 1970-01-01 if done by Migx but on the DB stays null.
Here is the field definition in Migx:
{
"MIGX_id":11916,
"field":"start_date",
"caption":"Data de inicio",
"description":"(Formato: AAAA-MM-DD)",
"description_is_code":"0",
"inputTV":"",
"inputTVtype":"date",
"validation":"",
"configs":{
"allowBlank":true,
"disabledDates":"0000-00-00",
"disabledDays":"",
"minDateValue":"",
"minTimeValue":"",
"maxDateValue":"",
"maxTimeValue":"",
"startDay":"",
"timeIncrement":"",
"hideTime":true
},
"restrictive_condition":"",
"display":"none",
"sourceFrom":"config",
"sources":"",
"inputOptionValues":"",
"default":"none",
"useDefaultIfEmpty":"0",
"pos":6
}
I am not really sure what more I can do.
Thanks in advance for your time.
nuan88
August 20, 2019, 2:32pm
6
I recognized that date, its apparently a default value (that converts to 0, it seems).
Ok I just googled, here is a thread about MIGX, with a workaround by the OP
opened 10:56PM - 19 Apr 13 UTC
I'm not sure where this is coming from exactly, because I have not seen the comb… ination of a date-tv and a "popup/overlay" (like the migx edit/update window) somewhere else (the quickedit window doesn't provide access to pub_date, forexample), so I cannot see how it's done there.
basically it's a two way problem:
1) Unix timestamps that are saved in the db now render correctly in the migxdb grid (as of commit https://github.com/Bruno17/MIGX/pull/80) but they don't in the update overlay when a field is specified as a date tv (date and time, like pub_date or so)...it just always shows 1.1.1970 12:00am, no matter what I try =/. The timestamp is a correct one and it also displays the right one when I switch to a normal text tv, so it's there and it does get read, but the migx update overlay doesn't seem to understand it, probably because it tries to make a Date.parseDate('Y-m-d H:m:i') or so and not read the time as a unix timestamp (I think same problem as with the grid rendering)
I know the problematic part it's located in the templates/mgr/fields.tpl at line 106 where {$tv->get('formElement')}
which creates this in the manager when fetching the form elements of the update overlay, and there lies the problem:
``` javascript
Ext.onReady(function() {
var fld = MODx.load({
xtype: 'xdatetime'
,applyTo: 'tv1991'
,name: 'tv1991'
,dateFormat: MODx.config.manager_date_format
,timeFormat: MODx.config.manager_time_format
,dateWidth: 120
,timeWidth: 120
,allowBlank: true ,value: '1970-01-01 01:00:00'
,msgTarget: 'under'
,listeners: { 'change': { fn:MODx.fireResourceFormChange, scope:this}}
});
Ext.getCmp('modx-panel-resource').getForm().add(fld);
});
```
which seems to come from /manager/templates/default/element/tv/renders/input/date.tpl, so a native modx thing...= bad = means we have to fix the passed value before that point...
really had to search, but found the right code in core/components/migx/processors/mgr/default/fields.php
at line 60 where
``` php
$record = $object->toArray();
// added this ugly thing...but like this it works...but naturally only if the field is called timestamp, so reeeeally ugly,
// sorry, didn't know better =/
if ( isset($record['timestamp']) ) {
$record['timestamp'] = strftime('%Y-%m-%d %H:%M:%S', $record['timestamp']);
}
```
2) The issue also shows it's ugly face when saving a record from the update overlay, so if for example the default value of the date tv is set to "now" (gives the current date/time) I can see in the post that this is sent as a date string in the Y-m-d H:m:i format and not as a unix timestamp (as I have seen in the resource pub_date/unpub_date fields in the normal modx db, date/time values are stored as timestamps), so what happens is, the mgr/default/update.php processor which does a $modx->fromJSON conversion fo the passed POST also only gets a date/time-string as this is what's sent to the script.
When the db field "timestamp" is an integer, the update.php processor cannot write into that field (because it want's to write a string into an int field...), so this is set to default of 0, what causes the 1.1.1970 12:00am...
for this second side of the problem I found a really ugly, hacky workaround by just converting the date/time-string to a unix timestamp in the update.php processor after line 69 at core/components/migx/processors/mgr/default/update.php like this:
``` php
if (isset($scriptProperties['data'])) {
$scriptProperties = array_merge($scriptProperties, $modx->fromJson($scriptProperties['data']));
}
// ugly shit, only works when field is called timestamp
if ( isset($scriptProperties['timestamp']) ) {
$scriptProperties['timestamp'] = strtotime($scriptProperties['timestamp']);
}
```
yeah, don't tell me it's ugly =D, I know, but at least, now the right date is SAVED to the DB (if I don't do this, my db timestamps get messed up every time someone edits a record...then its suddenly 0^^)
so I can work now with it btw. with timestamps, but I think this really is a problem...is nobody doing that and occured the same problems, or am I just a moron??? =)
Here also is more info, the case is different but this is where someone says that date converts to zero, and some of the workaround might be useful for you
opened 03:48PM - 04 Aug 14 UTC
closed 08:27PM - 29 Dec 18 UTC
type-frontend
bug
area-core
The `Date of birth` field in create/edit user form is kind of broken. If you use… the date picker or entering the value manually it always changes to this format: yyyy, mm-dd
If you hit `Save`, the field error tells you: `Please state the birthdate in mm/dd/yyyy format`
If you try to enter the date manually you get this field error while typing: `10/05/ is not a valid date - it must be in the format Y, m-d`
The `manager_date_format` in system settings is set to: `Y, m-d` (I think this was the default value)
This thing is trying to kidding me! :-)
It's impossible to enter a value!
I think, if you need dates, you will want to save them as part of your processing. I think this date is appearing because there is not a date saved for that item, but I could be wrong about that.
Searching Modx “1970-01-01” also has more hits
Edit: This looks like a solution from BobRay on the first page, to fix the date of tv creation for every tv.
1 Like
Thanks @nuan88 , let me try to explore these.
I will share the outcome here.
Cheers and many thanks
nuan88
August 21, 2019, 12:12am
8
I think I speak for everyone in the community when I say, we are always here
2 Likes