3
votes

I have trouble saving my date field into database using CakePHP.

Table column name

[User].[dob]

View

<?php echo $this->Form->input('dob', array('type'=> 'date', 'label' => FALSE, 'dateFormat' => 'DMY', 'minYear' => date('Y') - 111, 'maxYear' => date('Y'))); ?>

I get the following error when I submit the form -

2011-12-29 00:33:57 Debug: Notice (8): Array to string conversion in [C:\xampp\htdocs\dearmemoir\cake\libs\router.php, line 1573]

This field is part of the Auth User Model. Any ideas what might be going wrong?

2
You're concatenating an Array like it were a string, I think. Try to replace that with var_dump(...); to see what you're really returning there. - Aram Kocharyan
Also, date('Y') - 111 is a string - an int, maybe try intval(date('Y')) - 111 ? No, my bad, it works! - Aram Kocharyan
Ah, I think what has happened is that an Array has been passed as an argument in your args array, and cake is trying to concatenate it like a string. - Aram Kocharyan
I replaced [date('Y') - 111] with 1900 and still see the same error! - jagamot
Yeah, it turns out PHP won't mind there, but I'd try to do: <?php var_dump( $this->Form->input('dob', array('type'=> 'date', 'label' => FALSE, 'dateFormat' => 'DMY', 'minYear' => date('Y') - 111, 'maxYear' => date('Y'))) ); ?>. Is this the method: api.cakephp.org/class/form-helper#method-FormHelperinput - Aram Kocharyan

2 Answers

2
votes

This line of code did the magic for me -

$this->data['User']['dob'] = date('Y-m-d', strtotime($this->data['User']['dob']));

I am able to save data now!

0
votes

I suspect that you are looking in the wrong place. The error message is coming from the CakePHP routing (router.php) - i.e. possibly the redirect URL that you are using.

The example code you give looks correct, it almost exactly matches an example from the Cake cookbook:

echo $this->Form->input('birth_dt', array( 'label' => 'Date of birth', 'dateFormat' => 'DMY', 'minYear' => date('Y') - 70, 'maxYear' => date('Y') - 18 ));

You can add in a check for any validation errors.