Reputation: 1063
i am using cakephp 3.x, i have one form in which one field is of date. in backend i am using mysql.
my field structure in mysql is dob
of type date
.
now in cakephp 3.x i had use below syntax to create input.
echo $this->Form->input('dob', array(
'label' => (__('Date of Birth')),
'type' => 'text',
'required' => false,
'class' => 'form-control date'
));
and i had used bootstrap datetimepicker like,
$('.date').datetimepicker({
format: 'YYYY-MM-DD'
});
now when i submit the form at and i print_r
the request data at that time i got this field like this
[
....
'dob' => '2016-02-11',
....
]
but when i save the record and look in database then it show me random date like 2036-10-25
can anyone help me please?
Upvotes: 4
Views: 1476
Reputation: 1063
and this is the Final General Solution,
//File : src/Model/Table/PatientsTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Event\Event;
use ArrayObject;
use Cake\I18n\Time;
class PatientsTable extends Table
{
...
...
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
if (isset($data['dob'])) {
$data['dob'] = Time::parseDate($data['dob'], 'Y-M-d');
}
}
}
Upvotes: 1
Reputation: 346
You declared dob type as date but tried to save date in string format instead of date time format. Try this
use Cake\I18n\Time;
$this->request->data['dob']= Time::parseDate($this->request->data['dob'],'Y-M-d');
Upvotes: 0