centerHistoryRepository->findBy(['person' => $from]); foreach ($centerHistoriesA as $ch) { if ($oldestDateA === null || ($ch->getStartDate() < $oldestDateA)) { $oldestDateA = $ch->getStartDate(); $oldestCenterHistoryA = $ch; } } $centerHistoriesB = $this->centerHistoryRepository->findBy(['person' => $to]); foreach ($centerHistoriesB as $ch) { if ($oldestDateB === null || ($ch->getStartDate() < $oldestDateB)) { $oldestDateB = $ch->getStartDate(); $oldestCenterHistoryB = $ch; } } $sqlDelete = sprintf(<<<'SQL' DELETE FROM chill_person_person_center_history WHERE person_id = %d; SQL, $from->getId()); $sqlStatements = [$sqlDelete]; if ((null !== $oldestDateA && null !== $oldestDateB) && $oldestDateA <= $oldestDateB) { $sqlInsert = sprintf(<<<'SQL' UPDATE chill_person_person_center_history SET startDate = '%s' WHERE id = %d; SQL, $oldestDateA->format('Y-m-d'), $oldestCenterHistoryB->getId()); $sqlStatements = [$sqlInsert, $sqlDelete]; } return $sqlStatements; } }