如果该实体存在(我可以使用),我会尝试更新此功能,如果该实体不存在,请尝试插入该功能
private function updateDatabaseAnswer($client, $survey, $answer, $question){
$storage = $this->di->find('storage');
$query = "select sa.id "
."from survey_answers as sa "
."join survey_database_answers as sda "
."on sa.id = sda.id "
."join answers as a "
."on a.id = sda.id_answer "
."where id_client= ".$client->getId()." "
."and id_question = ".$question->getId()." "
."and id_survey = ".$survey->getId();
$result = $storage->executeSQL($query);
$updated_answer = new SurveyDatabaseAnswer();
if(!empty($result))
$updated_answer->setId($result[0]);
$updated_answer->setClient($client);
$updated_answer->setSurvey($survey);
$updated_answer->setAnswer($answer);
empty($result)?$storage->persist($updated_answer):$storage->merge($updated_answer);
$storage->flush();
}
如果实体没有退出,我会收到此消息
Type: Doctrine\DBAL\Exception\UniqueConstraintViolationException
Message: An exception occurred while executing 'INSERT INTO survey_database_answers (id, id_answer) VALUES (?, ?)' with params [7438573, 1533]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '7438573' for key 'PRIMARY'
尝试插入一个已经存在的ID