У меня есть 5 полей ввода, когда пользователь заполняет эти 5 полей и нажимает на кнопку отправить, мы должны сохранить их в базе данных
Для этого у меня есть этот код в моем контроллере
public function send()
{
$this->loadmodel('Invitefriend');
$this->request->is('post');
$this->Invitefriend->create();
$this->loadModel('Student');
$id = $this->userValue['Student']['id'];
$contact = $this->userValue['Student']['phone'];
$emails= $this->data['Invitefriends'];
$currentDate=date('Y-m-d',strtotime($this->currentDate));
$email_count = count($emails);
for($i=1;$i<=$email_count;$i++)
{
$email = $emails['email'.$i];
$this->Invitefriend->save(array("ref_student_id"=>$id, "ref_contact"=>$contact, 'email'=>$email, 'date'=>$currentDate));
}
}
С вставляет только последнее значение поля в базу данных, но мне нужно сохранить все 5 полей в базе данных. что-то не так в моем коде?
использование
// save multiple or all records
$this->Invitefriend->saveAll()
or
$this->Invitefriend->saveMany()
вместо
Invitefriend->save()
Попробуй это:
//Create entity for table...
$invTable = $this->Invitefriend->newEntity();
//Build db table fields...
$invTable->ref_student_id = $id;
$invTable->ref_contact = $contact;
$invTable->email = $email;
$invTable->date = $currentDate;
//Store data into table...
$this->Invitefriend->save($invTable);
Ваша функция выглядит так:
public function send() {
$this->loadmodel('Invitefriend');
$this->request->is('post');
$this->Invitefriend->create();
$this->loadModel('Student');
$id = $this->userValue['Student']['id'];
$contact = $this->userValue['Student']['phone'];
$emails= $this->data['Invitefriends'];
$currentDate=date('Y-m-d',strtotime($this->currentDate));
$email_count = count($emails);
//Create entity for table...
$invTable = $this->Invitefriend->newEntity();
//Build db table fields...
$invTable->ref_student_id = $id;
$invTable->ref_contact = $contact;
$invTable->date = $currentDate;
for($i=1;$i<=$email_count;$i++) {
$invTable->email = $emails['email'.$i];
//Store data into table...
$this->Invitefriend->save($invTable);
}
}