mysql - CakePHP 3 time column gets date added -


i have number of columns defined in mysql database "time". is, have time, not date. when read cakephp 3 orm, being converted cake\i18n\time objects (through cake\database\type\timetype class), result has both date , time in it, date set current date. example, if value "20:00:00", debug($record['start_time']) generate:

object(cake\i18n\time) {     'time' => '2015-06-21t20:00:00+0000',     'timezone' => 'utc',     'fixednowtime' => false } 

and when echo in template (without having used settostringformat), 6/21/15 8:00 pm. of course, can use $this->time->format put time-only format, seems strange need so. why cake ignoring fact just time, and, more importantly, there way make stop?

date/time values being casted same base structure, datetime or datetimeimmutable object, , naturally date-only values have time value added (00:00:00), , time-only values come date (the current date).

cakephp uses specific subclasses depending on sql datatype,

  • \cake\i18n\time or \cake\i18n\frozentime time, timestamp, , datetime
  • \cake\i18n\date or \cake\i18n\frozendate date

in earlier cakephp 3 versions there \cake\i18n\time.

it nice if there separate class time-only types, have proper time-only default output format set, until added, you'll have take care of output format yourself.

format in views

it how display in views. can use i18nformat() method of time class instance

$record['start_time']->i18nformat(     [\intldateformatter::none, \intldateformatter::short] ) 

or time helper, show time part

$this->time->i18nformat(     $record['start_time'],     [\intldateformatter::none, \intldateformatter::short] ) 

guess wouldn't hurt if bake generate similar code according type of column, may want suggest enhancement. mentioned using additional classes (or maybe options) time-only columns may worth consider too.

use custom time class

if you'd wanted behavior everywhere string representation of object being used, without having manually invoke formatter, make use of extended \cake\i18n\time or \cake\i18n\frozentime class overriden $_tostringformat property, formats date accordingly.

src/i18n/frozentimeonly.php

namespace app\i18n;  use cake\i18n\frozentime;  class frozentimeonly extends frozentime {     protected static $_tostringformat = [         \intldateformatter::none,         \intldateformatter::short     ]; } 

src/config/bootstrap.php

use cake\database\type\timetype; use app\i18n\frozentimeonly; timetype::$datetimeclass = frozentimeonly::class;  // remove default `useimmutable()` call, may // want keep further calls formatting , stuff type::build('time');  // ... 

this should pretty self-explantory, time columns being mapped timetype, use app\i18n\frozentimeonly instead of default cake\i18n\time.

datetimetype::$datetimeclass deprecated

in order cope that, custom database type required, rather simple too.

src/database/type/timeonlytype.php

namespace app\database\type;  use app\i18n\frozentimeonly; use cake\database\type\timetype;  class timeonlytype extends timetype {     public function __construct($name)     {         parent::__construct($name);         $this->_setclassname(frozentimeonly::class, \datetimeimmutable::class);     } } 

it should note instantiate data/time class twice, parent constructor invoke _setclassname() too, instance of given class instantiated.

src/config/bootstrap.php

use app\database\type\timeonlytype; type::map('time', timeonlytype::class); 

so do, override default time type mapping use custom \app\database\type\timeonlytype class, in turn use \app\i18n\timeonly class when converting database values php objects, when converted string, use time-only format.

see also


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -