3

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?

8
  • 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. Commented Dec 29, 2011 at 5:43
  • Also, date('Y') - 111 is a string - an int, maybe try intval(date('Y')) - 111 ? No, my bad, it works! Commented Dec 29, 2011 at 5:45
  • 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. Commented Dec 29, 2011 at 5:47
  • I replaced [date('Y') - 111] with 1900 and still see the same error! Commented Dec 29, 2011 at 6:04
  • 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 Commented Dec 29, 2011 at 6:06

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Where did you added this code. I mean which file and where.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.