Saturday, June 13, 2015

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

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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: