CakePHP3で開発中にDate型カラムを持つEntityに
Formからinput[type=date]で日付の値を渡したところ、全然違う日付が表示されてしまった。
例として、2016-02-22という日付を送ったところ、2189-12-02という日付になり
何がどうしてこうなったんだ?という状況に・・・。
いろいろ調べて、以下のコードをconfig/bootstrap.phpに追加することにより解決!
1 2 3 |
CakeI18nDate::$defaultLocale = 'ja-JP'; Type::build('date')->useLocaleParser(); Type::build('datetime')->useLocaleParser(); |
・・・のはずだった。
画面で確認してみると、日付が2016-02-21と表示され
一瞬、「おっ!できた!」と思ったものの、よく見ると日付が違う・・・。
なんでだろうなぁと、いろいろ悩んだ結果、CakePHP3のリリースノートで下記の記事を発見。
Bugfixes in 3.2.3
In addition to the security fix, 3.2.3 contains the following bug fixes:
- Camel-backed virtual properties can be accessed once again. This was a regression in 3.2.0. (@markstory)
- Long paths in the error pages are now wrapped. (@bcrowe)
_joinData
is no longer lost when marshalling existing BelongsToMany records using theid
attributes. (@markstory)Date::parseDate()
now correctly handles timezones. (@lorenzo)- Setting a Cell’s templatePath inside the action now works. (@markstory)
- FormHelper now consistently uses
formatTemplate()
internally. (@ypnos-web)Hash::sort()
now correctly sortsDateTimeImmutable
objects. (@ndm2)Security::randomBytes()
was added. This method wraps the various ways to get secure random data. If secure data cannot be found, insecure data will be used and a warning will be emitted. (@markstory)- CSRF tokens are now generated with
Security::randomBytes()
. (@markstory)- Custom template paths are now factored into view path generation. (@phillaf)
こちらの開発環境を調べてみたところ、CakePHP3のバージョンは3.2.1
なので、composerでupdateをかけてみたところ
無事正しい日付で表示されました。