Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

Aday Talavera
First off, sorry for the "question spam". I'm just posting all issues of the last two weeks in a short period of time. I hope that this isn't disturbing anyone.

While trying to figure how to use combine with collections and entities (is in other thread) I didn't found an easy way to avoid date / datetime fields of DB been converted to Time instances.

$query->hydrate(false);

Didn't work, date / datetime field is also converted to a Cake/Time instance

There exists an easy way to avoid that conversion?

I was forced to do something like this in the collection to use combine or indexBy groupBy collection functions with date fields:

            ->each(function ($entity){
                $entity
->dateStr = $entity->date->toDateString();
               
return $entity;
           
})
            ->combine('dateStr', 'id')
            ->indexBy('dateStr')
           
->GroupBy('dateStr')

Cheers.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

Aday Talavera
Up!

El miércoles, 13 de mayo de 2015, 21:33:08 (UTC+1), Aday Talavera escribió:
First off, sorry for the "question spam". I'm just posting all issues of the last two weeks in a short period of time. I hope that this isn't disturbing anyone.

While trying to figure how to use combine with collections and entities (is in other thread) I didn't found an easy way to avoid date / datetime fields of DB been converted to Time instances.

$query->hydrate(false);

Didn't work, date / datetime field is also converted to a Cake/Time instance

There exists an easy way to avoid that conversion?

I was forced to do something like this in the collection to use combine or indexBy groupBy collection functions with date fields:

            ->each(function ($entity){
                $entity
->dateStr = $entity->date->toDateString();
               
return $entity;
           
})
            ->combine('dateStr', 'id')
            ->indexBy('dateStr')
           
->GroupBy('dateStr')

Cheers.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

Aday Talavera
In reply to this post by Aday Talavera
It seems that the only way is to change column schema type from 'date' to 'string'.

Maybe could be a good idea to have a function to do such changes on an easy way.

El miércoles, 13 de mayo de 2015, 21:33:08 (UTC+1), Aday Talavera escribió:
First off, sorry for the "question spam". I'm just posting all issues of the last two weeks in a short period of time. I hope that this isn't disturbing anyone.

While trying to figure how to use combine with collections and entities (is in other thread) I didn't found an easy way to avoid date / datetime fields of DB been converted to Time instances.

$query->hydrate(false);

Didn't work, date / datetime field is also converted to a Cake/Time instance

There exists an easy way to avoid that conversion?

I was forced to do something like this in the collection to use combine or indexBy groupBy collection functions with date fields:

            ->each(function ($entity){
                $entity
->dateStr = $entity->date->toDateString();
               
return $entity;
           
})
            ->combine('dateStr', 'id')
            ->indexBy('dateStr')
           
->GroupBy('dateStr')

Cheers.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

José Lorenzo
There are a few ways to accomplish this:

First one:

// ArticlesTable.php
public function _initializeSchema($schema) {
    $schema->columnType('my_date_column', 'string');
}

That will remove all the converting back and forth for that specific column. It will still be a date in the database, but cake will always treat it as a string


Second One:
// bootstrap.php

\Cake\Database\Type::build('date', 'Cake\Database\Type');

This other option will disable the converting in the whole application.... But in any case, what are the reasons for disabling it?

On Friday, June 12, 2015 at 12:17:37 PM UTC+2, Aday Talavera wrote:
It seems that the only way is to change column schema type from 'date' to 'string'.

Maybe could be a good idea to have a function to do such changes on an easy way.

El miércoles, 13 de mayo de 2015, 21:33:08 (UTC+1), Aday Talavera escribió:
First off, sorry for the "question spam". I'm just posting all issues of the last two weeks in a short period of time. I hope that this isn't disturbing anyone.

While trying to figure how to use combine with collections and entities (is in other thread) I didn't found an easy way to avoid date / datetime fields of DB been converted to Time instances.

$query->hydrate(false);

Didn't work, date / datetime field is also converted to a Cake/Time instance

There exists an easy way to avoid that conversion?

I was forced to do something like this in the collection to use combine or indexBy groupBy collection functions with date fields:

            ->each(function ($entity){
                $entity
->dateStr = $entity->date->toDateString();
               
return $entity;
           
})
            ->combine('dateStr', 'id')
            ->indexBy('dateStr')
           
->GroupBy('dateStr')

Cheers.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

Aday Talavera
Following my example and you answer in https://groups.google.com/forum/?hl=es#!topic/cake-php/zUwDGhkMFO4 this could be achieved like this:

combine(function($entity){return $entity->date->toDateString();}, 'id')



El sábado, 13 de junio de 2015, 8:17:56 (UTC+1), José Lorenzo escribió:
There are a few ways to accomplish this:

First one:

// ArticlesTable.php
public function _initializeSchema($schema) {
    $schema->columnType('my_date_column', 'string');
}

That will remove all the converting back and forth for that specific column. It will still be a date in the database, but cake will always treat it as a string


Second One:
// bootstrap.php

\Cake\Database\Type::build('date', 'Cake\Database\Type');

This other option will disable the converting in the whole application.... But in any case, what are the reasons for disabling it?

On Friday, June 12, 2015 at 12:17:37 PM UTC+2, Aday Talavera wrote:
It seems that the only way is to change column schema type from 'date' to 'string'.

Maybe could be a good idea to have a function to do such changes on an easy way.

El miércoles, 13 de mayo de 2015, 21:33:08 (UTC+1), Aday Talavera escribió:
First off, sorry for the "question spam". I'm just posting all issues of the last two weeks in a short period of time. I hope that this isn't disturbing anyone.

While trying to figure how to use combine with collections and entities (is in other thread) I didn't found an easy way to avoid date / datetime fields of DB been converted to Time instances.

$query->hydrate(false);

Didn't work, date / datetime field is also converted to a Cake/Time instance

There exists an easy way to avoid that conversion?

I was forced to do something like this in the collection to use combine or indexBy groupBy collection functions with date fields:

            ->each(function ($entity){
                $entity
->dateStr = $entity->date->toDateString();
               
return $entity;
           
})
            ->combine('dateStr', 'id')
            ->indexBy('dateStr')
           
->GroupBy('dateStr')

Cheers.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
glk
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

glk
In reply to this post by Aday Talavera
@JoseLorenzo

The reason is that the cake $this->Form->input('date_field') always generates 3 input fields.  If the input uses type=text, the field is populated with mm/dd/yyyy, hh:mm:ss
And all we should have is mm/dd/yyyy... of course it should also be LOCALE format.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

José Lorenzo
It is an unimplemented feature... Someone need to solve that issue of showing time in a date column

On Friday, October 23, 2015 at 9:56:34 PM UTC+2, glk wrote:
@JoseLorenzo

The reason is that the cake $this->Form->input('date_field') always generates 3 input fields.  If the input uses type=text, the field is populated with mm/dd/yyyy, hh:mm:ss
And all we should have is mm/dd/yyyy... of course it should also be LOCALE format.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.
glk
Reply | Threaded
Open this post in threaded view
|

Re: Avoid converting date/datetime fields to Cake/Time instances with CakePHP 3

glk
Jose,

It turns out that I AM MISTAKEN...  The input field I was looking at was defined as a "datetime".

However...  The real issue is that the datetime input field is populated by cake always as yyyy-mm-dd hh:mm:ss  If it is a date object, it is yyy-mm-dd.  If I am in locale en-US and have added the following to the boostrap.php
DispatcherFactory::add('LocaleSelector', ['locales' => ['en_US']]);

Type::build('datetime')->useLocaleParser();
Type::build('date')->useLocaleParser()->setLocaleFormat(['MM/dd/yyyy', \IntlDateFormatter::NONE]);
Type::build('time')->useLocaleParser()->setLocaleFormat([\IntlDateFormatter::NONE, 'hh:mm a']);

Then, the parser on any attempt to save the data will die... returns null... and then attempts to use null as a pointer.

If I change the parser to they mysql yyy-mm-dd or remove the parser altogether, then it works.  All of this is just to keep from using 3 fields to enter a date, or 6 fields to enter a datetime!  I want to use a js datepicker and have LOCALE style inputs.

In DateTimeType.php the "marshal" function uses sprintf() to format the date/time and probably should be using  a "time->i18nFormat()" to get the default user time shown and then the parser should use new Time( $input_str, USER_TIMEZONE) to parse the string... it accepts multiple types of input strings and "figures" out the order of MDY.  Then it should store the time in the yyy-mm-dd hh:mm:ss converted to SERVER_TIMEZONE.

The use of setLocaleFormat just complicates everything... If the i18n->locale is set, Time:: takes care of parsing the string!

Sorry for all the blather, I'm just trying to explain what I think it should be doing.  Again, I'm sorry for mistakenly stating that the time is always output for a "date" field.  By the way, "time" fields are also fixed format in the marshaller!



On Monday, October 26, 2015 at 4:36:21 AM UTC-6, José Lorenzo wrote:
It is an unimplemented feature... Someone need to solve that issue of showing time in a date column

On Friday, October 23, 2015 at 9:56:34 PM UTC+2, glk wrote:
@JoseLorenzo

The reason is that the cake $this->Form->input('date_field') always generates 3 input fields.  If the input uses type=text, the field is populated with mm/dd/yyyy, hh:mm:ss
And all we should have is mm/dd/yyyy... of course it should also be LOCALE format.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.