cakephp: `DateTimeType::marshal()` occasionally returns string instead of object

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 3.3.11

What you did

Create a form containing a text input field for a datetime property.

echo $this->Form->create($article);
echo $this->Form->input('published', ['type' => 'text']);
echo $this->Form->button('Submit');
echo $this->Form->end();

Dump the datetime property after patchEntity().

$article = $this->Articles->get(1);
$this->Articles->patchEntity($article, $this->request->data);
if ($this->Articles->save($article)) {
    var_dump($article->published);
}
$this->set('article', $article);

What happened

When the posted value is 2017-01-01 00:00:00, I get:

object(Cake\I18n\FrozenTime)#156 (3) {
  ["time"]=>
  string(25) "2017-01-01T00:00:00+09:00"
  ["timezone"]=>
  string(10) "Asia/Tokyo"
  ["fixedNowTime"]=>
  bool(false)
}

However, when the posted value is 2017-01-01 00:00, I get:

string(16) "2017-01-01 00:00"

What you expected to happen

I always get:

object(Cake\I18n\FrozenTime)#156 (3) {
  ["time"]=>
  string(25) "2017-01-01T00:00:00+09:00"
  ["timezone"]=>
  string(10) "Asia/Tokyo"
  ["fixedNowTime"]=>
  bool(false)
}

Related code

When the input format is not Y-m-d H:i:s, DateTimeType::marshal() returns a string. https://github.com/cakephp/cakephp/blob/3b341696e13ad6aed51e330d8e54479a41780512/src/Database/Type/DateTimeType.php#L164-L166

The following line would also return a string. https://github.com/cakephp/cakephp/blob/3b341696e13ad6aed51e330d8e54479a41780512/src/Database/Type/DateTimeType.php#L170-L172

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 37 (30 by maintainers)

Commits related to this issue

Most upvoted comments

I think the value is taken from the request, not the entity when rendering the from in a POST request. Am I remembering incorrectly?