I tried to add new data for start date & end date. The data entered the array but somehow there’s error “Call to a member function format() on null” at index.php, line 33.
This is my SaleController Function Add:
public function add()
{
$sale = $this->Sales->newEmptyEntity();
// pr($sale);die;
// $data = $this->request->getData();
// pr($data);exit;
if ($this->request->is('post')) {
$data = $this->request->getData();
// pr($data);exit;
$date['start_date'] = !empty($data['start_date']) ? date('d-m-Y', strtotime($data['start_date'])) : '';
$date['end_date'] = !empty($data['end_date']) ? date('d-m-Y', strtotime($data['end_date'])) : '';
// $start_date = !empty($data['start_date']) ? date('Y-m-d', strtotime($sale->start_date->format('Y-m-d H:i:s')));
// $end_date = !empty($data['end_date']) ? date('Y-m-d', strtotime($sale->end_date->format('Y-m-d H:i:s')));
$sale = $this->Sales->patchEntity($sale, $data);
if ($this->Sales->save($sale)) {
$this->Flash->success(__('The sale has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The sale could not be saved. Please, try again.'));
}
$vehicles = $this->Sales->Vehicles->find('list', ['limit' => 200]);
$users = $this->Sales->Users->find('list', ['limit' => 200]);
$this->set(compact('sale', 'vehicles', 'users'));
}