Dear all,
I want to add many records one by one.
I have an Add form, and want to comeback after Submit, however, the form is full of previous data.
I have search but did not find solution.
I tried creating empty entity for the form, but it seems not enough, people say that I have to clear request data. But I dont know how.
Below is my Add action in Controller
public function add($parentID = null)
{
$kid = $this->Kids->newEmptyEntity();
if ($this->request->is('get')) {
$pageReferer = $this->referer();
}
if ($this->request->is('post')) {
$kid = $this->Kids->patchEntity($kid, $this->request->getData());
$this->loadModel('IcsParents');
$icsParent = $this->IcsParents->get($this->request->getData('parentID'), ['fields' => ['id', 'family_id']]);
if (is_null($icsParent->family_id)){
//create family
$family = $this->Kids->Families->newEmptyEntity();
$family->consultant_id = $this->Auth->user('id');
if ($this->Kids->Families->save($family)){
$icsParent->family_id = $family->id;
$this->IcsParents->save($icsParent);
$kid->family_id = $family->id;
}
} else {
$kid->family_id = $icsParent->family_id;
}
if ($this->Kids->save($kid)) {
$this->Flash->success(__('Đã thêm bé vào Family.'));
if ($this->request->getData('submit') == 1) {
$kid = $this->Kids->newEmptyEntity();
$pageReferer = $this->request->getData('pageReferer');
//$this->request->withoutData('kid'); // I dont know what this statement do, just try
} else {
return $this->redirect($this->request->getData('pageReferer'));
}
} else {
$this->Flash->error(__('Không thể thêm bé vào Family. Vui lòng kiểm tra các thông tin nhập vào.'));
}
}
$nationalities = $this->Kids->Nationalities->find('list', ['limit' => 200]);
$eduTypes = $this->Kids->EduTypes->find('list', ['limit' => 200]);
$this->set(compact('kid', 'parentID', 'nationalities', 'eduTypes', 'pageReferer'));
}