Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony] Symfony2 - The EntityManager is closed
Forum PHP.pl > Forum > PHP > Frameworki
Ghost_78
Witam serdecznie.
Mam dzisiaj następujący problem. Od jakiegoś czasu bawię się z Symfony 2.
Mój problem jest następujący. Napisałem sobie komendę do crona. W skrócie wygląda to tak.

  1. class CronCommand extends ContainerAwareCommand
  2. {
  3. protected $em;
  4. protected $log;
  5.  
  6. protected function configure()
  7. {
  8. $this->setName('cron:go')
  9. ->setDescription('update collection ranking');
  10. }
  11.  
  12. protected function execute(InputInterface $input, OutputInterface $output)
  13. {
  14. $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
  15. $this->log = $this->em->getRepository('AppsBaseBundle:Log');
  16. $minutes = date('i');
  17. $hours = date('H');
  18.  
  19. //$this->all();
  20. //if($minutes == '00'){
  21. $this->fullHour();
  22. //}
  23. if($hours == '00' && $minutes == '00'){
  24. $this->endOfTheDay();
  25. }
  26.  
  27. }
  28.  
  29. protected function fullHour(){
  30. try{
  31. $Ranking = new \Apps\BaseBundle\Util\Event\Ranking();
  32. $Ranking->actualizeAll($this->em,\Apps\BaseBundle\Util\Event\Ranking::DAILY);
  33. $comm = 'Zaktualizowany ranking godzinowy';
  34. echo $comm."\n";
  35. $this->log->addLog(__CLASS__.':'.__FUNCTION__,
  36. $comm,
  37. Log::NOTIFY);
  38. } catch (\Exception $e){
  39. echo $e->getMessage()."\n";
  40. $this->log->addLog(__CLASS__.':'.__FUNCTION__,
  41. 'Wyjątek: '.$e->getMessage(),
  42. Log::EXCEPTION);
  43. }
  44. }


Problem jest następujący. Po odpaleniu tego z konsoli na jednym komputerze wszystko działa a na drugim (te same wszystkie pliki) dostaję komunikat:
The EntityManager is closed.

Bladego pojęcia nie wiem w czym może być problem.
Będę bardzo wdzięczny za wszelkie wskazówki.

Pozdrawiam.

[EDIT]
Dodam tylko, że nigdzie nie zamykam EM.
ano
No to jeszcze pokaż metody:
\Apps\BaseBundle\Util\Event\Ranking::actualizeAll
AppsBaseBundle:Log::addLog
CronCommand::endOfTheDay
Ghost_78
Nie ma sprawy.

\Apps\BaseBundle\Util\Event\Ranking::actualizeAll
  1. public function actualizeAllForContent(\Apps\BaseBundle\Entity\Content $Content,
  2. \Doctrine\ORM\EntityManager $em,
  3. $type = 'all'){
  4. if($type == 'all' || $type == Ranking::DAILY){
  5. $this->actualizeDailyPoints($Content, $em);
  6. }
  7. if($type == 'all' || $type == Ranking::WEEKLY){
  8. $this->actualizeWeeklyPoints($Content, $em);
  9. }
  10. if($type == 'all' || $type == Ranking::MONTHLY){
  11. $this->actualizeMonthlyPoints($Content, $em);
  12. }
  13. }


\Apps\BaseBundle\Util\Event\Ranking::actualizeDailyPoints
  1. public function actualizeDailyPoints(\Apps\BaseBundle\Entity\Content $Content,
  2. \Doctrine\ORM\EntityManager $em){
  3. try{
  4. $contentRankingRepo = $em->getRepository('AppsBaseBundle:ContentRanking');
  5. $contentRankingRepo->actualizeDailyPoints($Content);
  6. } catch (\Exception $e){
  7. echo $e->getMessage();
  8. }
  9. }


AppsBaseBundle:ContentRanking->actualizeDailyPoints
  1. function actualizeDailyPoints(\Apps\BaseBundle\Entity\Content $Content){
  2. $em = $this->getEntityManager();
  3. $Rankimg = new Ranking();
  4. $daylyPoints = $Rankimg->getSumForDay($Content, $this->getEntityManager());
  5. $Ranking = $this->findOneBy(array('content_id'=>$Content->getId(),
  6. 'type'=> Ranking::DAILY,
  7. 'date'=>new \DateTime('now')));
  8. if(! $Ranking){
  9. $Ranking = new \Apps\BaseBundle\Entity\ContentRanking();
  10. $Ranking->setContentId($Content->getId());
  11. $Ranking->setDate(new \DateTime('now'));
  12. $Ranking->setType(Ranking::DAILY);
  13. }
  14. $Ranking->setPoints($daylyPoints);
  15. $em = $this->getEntityManager();
  16. $em->persist($Ranking);
  17. $em->flush();
  18. //echo $points;
  19. }


Log (Repository)
  1. class LogRepository extends EntityRepository{
  2.  
  3. public function addLog($place,$description,$type){
  4. $Log = new \Apps\BaseBundle\Entity\Log();
  5. $Log->setDateTime(new \DateTime('now'));
  6. $Log->setType($type);
  7. $Log->setPlace($place);
  8. $Log->setDescription($description);
  9. $em = $this->getEntityManager();
  10. $em->persist($Log);
  11. $em->flush();
  12. }
  13. }


CronCommand::endOfTheDay - jest tu bez znaczenia - wykonuje się raz na dobę - o północy. Przy testowaninu nie jest uruchamiany.

Ok. Znalazlem problem. Zostawie dla potomnych.

Okazało się, że w metodzie:
AppsBaseBundle:ContentRanking->actualizeDailyPoints

  1. function actualizeDailyPoints(\Apps\BaseBundle\Entity\Content $Content){
  2. $em = $this->getEntityManager();
  3. $Rankimg = new Ranking();
  4. $daylyPoints = $Rankimg->getSumForDay($Content, $this->getEntityManager());
  5. $Ranking = $this->findOneBy(array('content_id'=>$Content->getId(),
  6. 'type'=> Ranking::DAILY,
  7. 'date'=>new \DateTime('now')));
  8. if(! $Ranking){
  9. $Ranking = new \Apps\BaseBundle\Entity\ContentRanking();
  10. $Ranking->setContentId($Content->getId());
  11. $Ranking->setDate(new \DateTime('now'));
  12. $Ranking->setType(Ranking::DAILY);
  13. }
  14. $Ranking->setPoints($daylyPoints);
  15. $em = $this->getEntityManager();
  16. $em->persist($Ranking);
  17. $em->flush();
  18. //echo $points;
  19. }


w tym wierszu:

  1. $Ranking->setContentId($Content->getId());


podawałem ID contentu. Niestety mam też zmapowaną relację do Contentu. Wyglądało to najprawdopodobniej tak, że ja mu podałem ID a on go nie wiadomo dlaczego nie zapisał.
Zamieniłem ten wpis na:

  1. $Ranking->setContent($Content);


i wszystko lata.

Pozdrawiam szanowne grono.
ano
Masz źle zrobione mapowania. W pliku z mapowaniem/adnotacjach nie rób samemu pola klasy 'content_id'. Jedynie pole content z ustalonym powiązaniem. Częsty błąd.
Ghost_78
Rozumiem.
Dobra rada/uwaga. Zapamiętam. smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.