I have setup a diary service for my application. The idea was to inject the service into my controllers (though a factory) and then call the diaryEntry method as required to store diary entries into the dB. The issue I am having is I am only getting the last entry stored in the dB if I call the method more than once in a controller action. Here is DiaryService::diaryEntry -
public function diaryEntry($projectUrn, $msg, $diaryCategory = 14)
{
$em = $this->em;
$user = $this->user;
$ProjectDiary = $this->projectDiaryEntity;
if(is_numeric($projectUrn)) {
$projectUrn = $em->find('Application\Entity\ProjectUrn', $projectUrn);
}
$ProjectDiary->setProjectUrn($projectUrn);
$ProjectDiary->setDiaryCategory($em->find('Application\Entity\DiaryCategory', $diaryCategory));
$ProjectDiary->setStatus(1);
$ProjectDiary->setComment($msg);
$ProjectDiary->setUser($em->getRepository('Application\Entity\Users')->findOneBy(['username' => $user->username]));
$ProjectDiary->setTimestamp(new \DateTime());
$em->persist($ProjectDiary);
$em->flush();
return;
}
In a controller action I then have something like this:
$this->diaryService->diaryEntry($order->getProjectUrn(), 'test msg 1);
$this->diaryService->diaryEntry($order->getProjectUrn(), 'test msg 2);
The issue is only msg 2 is getting saved in the dB, msg 1 is not being saved. The last update was to add the flush at the end of the diaryEntry method but this has not made a difference. Does anyone have any ideas where I coudl start looking to solve this issue? Is it bad architecture and perhaps not suitable as a service and should utilise something else?
I am happy to paste any further code required from the app
Thank you in advance for any assistance