0

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

JamesB
  • 147
  • 1
  • 7

1 Answers1

1

It would help more if you post information about how $this->projectDiaryEntity is initialized, but I bet you inject diary entity object through factory, so all your operations are done on same object.

Try replace your line with this one: $ProjectDiary = clone $this->projectDiaryEntity;

Your method does not look good however it's hard to tell what you should change based on a few lines of one method.

If your default status is 1, you can set it in your entity class. Same goes with timestamp.

SzymonM
  • 904
  • 8
  • 14
  • Hi SzymonM, indeed I am injecting diary object through factory and what you say makes sense and it works!! Thank you kindly I have accepted and up voted. We had tried cloning the service in the controller etc but never in the actual service. – JamesB Mar 07 '17 at 15:11
  • 1
    **Beware**: PHP doesn't do deep cloning by default, This means if a property of a cloned entity points to another entity or object, it will be not cloned. You may want to check out [this answer](http://stackoverflow.com/questions/14158111/deep-clone-doctrine-entity-with-related-entities) and [this library](https://github.com/myclabs/DeepCopy). – edigu Mar 12 '17 at 11:29