public function createStatisticSnapshot() { $snapshot = new StatisticSnapshot(); $snapshot->setParts($this->getPartCount()); $snapshot->setCategories($this->getPartCategoryCount()); $unitCounts = $this->getUnitCounts(); $partUnitRepository = $this->entityManager->getRepository('PartKeeprPartBundle:PartMeasurementUnit'); foreach ($unitCounts as $unitCount) { $snapshotUnit = new StatisticSnapshotUnit(); $snapshotUnit->setPartUnit($partUnitRepository->findOneBy(['id' => $unitCount['partMeasurementUnit']['id']])); $snapshotUnit->setStatisticSnapshot($snapshot); if ($unitCount['stockLevel'] !== null) { $snapshotUnit->setStockLevel($unitCount['stockLevel']); } else { $snapshotUnit->setStockLevel(0); } $snapshot->getUnits()->add($snapshotUnit); } $this->entityManager->persist($snapshot); $this->entityManager->flush(); }