Неопределенная ошибка индекса при $ em-> clear () в Symfony2

Я написал команду Symfony для импорта некоторых данных из API. Это работает, но проблема в моем использовании памяти PHP увеличивается, когда я вставляю большой JSON в свою базу данных. И мой unitOfWork увеличивается на «2» до каждого импорта активности.

Я уже отключил все мои использованные объекты, и я прочитал документацию по Symfony2, когда вы хотите сделать массивную партию: http://www.doctrine-project.org/blog/doctrine2-batch-processing.html

Но когда я использую $em->clear() мой диспетчер сущности дает эту ошибку:

Примечание. Неопределенный индекс: 000000007b56ea7100000000e366c259 в пути к приложению \ vendor \ doctrine \ lib \ Doctrine \ ORM \ UnitOfWork.php 2228

Вот мой полный код:

  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } с  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } с  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } с  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } с  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } с  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } с  /** * @see Command */ protected function configure() { $this ->setName('ks:user:runkeepersync') ->setDescription('Synchroniser les activités d\'un utilisateur runkeeper') ->setDefinition(array( new InputArgument('access_token', InputArgument::REQUIRED, 'Access token'), )) } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $accessToken = $input->getArgument('access_token'); $em = $this->getContainer()->get('doctrine')->getEntityManager(); $UserHasServices = $em->getRepository('KsUserBundle:UserHasServices')->findOneByToken($accessToken); if (!is_object($UserHasServices) ) { echo "Impossible de trouver l'utilisateur qui possède le jeton ".$accessToken.""; } $user = $UserHasServices->getUser(); $service = $UserHasServices->getService(); echo "avant de requérir l'api : ".memory_get_usage()."\n"; try { $rkApi = $this->getContainer()->get('ks_user.runkeeper'); $rkApi->setAccessToken($accessToken); $activities = $rkApi->getFitnessActivities(0,25); $nbParPages = 25; $nomberActivitites = $activities->size; $aActivities = $activities->items; $nbPages = floor ($nomberActivitites/$nbParPages); $aEndurance = array("Running", "Cycling", "Mountain Biking", "Walking", "Hiking", "Downhill Skiing", "Cross-Country Skiing", "Snowboarding", "Skating","Wheelchair", "Rowing", "Elliptical", "Other"); $aEnduranceUnderWater = array("Swimming"); $enduranceOnEarthType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance"); if (!is_object($enduranceOnEarthType) ) { echo "Impossible de trouver le type de sport d'endurance"; } $enduranceUnderWaterType = $em->getRepository('KsActivityBundle:SportType')->findOneByLabel("endurance_under_water"); if (!is_object($enduranceUnderWaterType) ) { echo "Impossible de trouver le type de sport d'endurance sous l'eau "; } echo "Après avoir récupéré 25 activités : ".memory_get_usage()."\n"; $a = 0; for($i=0;$i<=$nbPages;$i++){ if($i!=0){ $activities = $rkApi->getFitnessActivities($i,25); $aActivities = $activities->items; } foreach ($aActivities as $activity) { $a = $a+1; $codeSport = $this->formatNameSport($activity->type); $sport = $em->getRepository('KsActivityBundle:Sport')->findOneByCodeSport($codeSport); if (!is_object($sport) ) { $sport = new \Ks\ActivityBundle\Entity\Sport(); $sport->setLabel($codeSport); $sport->setCodeSport($codeSport); $sport->setSportType($enduranceOnEarthType); $em->persist($sport); $em->flush(); } $activityDetail = json_decode($rkApi->requestJSONHealthGraph($activity->uri)); if(in_array($activity->type, $aEndurance)){ $urlActivitieDetail = $activityDetail->activity; $ActivitySessionEnduranceOnEarth = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceOnEarth($user); isset($activity->total_distance)? $ActivitySessionEnduranceOnEarth->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceOnEarth->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time)? $ActivitySessionEnduranceOnEarth->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceOnEarth->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceOnEarth->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceOnEarth->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->climb)? $ActivitySessionEnduranceOnEarth->setElevationGain($activityDetail->climb) : ""; $maxElevation = 0; $minElevation = 10000; if(isset($activityDetail->path)){ foreach($activityDetail->path as $gpsPoint){ if($gpsPoint->altitude > $maxElevation){ $maxElevation = $gpsPoint->altitude; } if($gpsPoint->altitude < $minElevation){ $minElevation = $gpsPoint->altitude; } } $ActivitySessionEnduranceOnEarth->setElevationMin($minElevation); $ActivitySessionEnduranceOnEarth->setElevationMax($maxElevation); } $em->persist($ActivitySessionEnduranceOnEarth); $em->flush(); //Pour chaque activité on a un identifiant relatif au service qu'on synchronise $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceOnEarth); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activite num ".$a." type :".$activity->type." effectue avec success \n"; unset($ActivitySessionEnduranceOnEarth); unset($ActivityComeFromService); echo "UnitOFWOrk -> ".$em->getUnitOfWork()->size()."\n"; } if(in_array($activity->type, $aEnduranceUnderWater)){ $ActivitySessionEnduranceUnderWater = new \Ks\ActivityBundle\Entity\ActivitySessionEnduranceUnderWater($user); isset($activity->total_distance)? $ActivitySessionEnduranceUnderWater->setDistance($activity->total_distance) : ""; isset($activity->duration)? $ActivitySessionEnduranceUnderWater->setDuration($this->secondesToTimeDuration($activity->duration)) : ""; isset($activity->start_time) && !empty($activity->start_time)? $ActivitySessionEnduranceUnderWater->setIssuedAt(new \DateTime($activity->start_time)) : ""; $ActivitySessionEnduranceUnderWater->setModifiedAt(new \DateTime('Now')); $ActivitySessionEnduranceUnderWater->setSport($sport); isset($activityDetail->total_calories)? $ActivitySessionEnduranceUnderWater->setCalories($activityDetail->total_calories) : ""; isset($activityDetail->notes)? $ActivitySessionEnduranceUnderWater->setDescription($activityDetail->notes) : ""; $em->persist($ActivitySessionEnduranceUnderWater); $em->flush(); $ActivityComeFromService = new \Ks\ActivityBundle\Entity\ActivityComeFromService(); $ActivityComeFromService->setActivity($ActivitySessionEnduranceUnderWater); $ActivityComeFromService->setService($service); $ActivityComeFromService->setIdWebsiteActivityService($activity->uri); $ActivityComeFromService->setSourceDetailsActivity($rkApi->requestJSONHealthGraph($activity->uri)); $ActivityComeFromService->setTypeSource("JSON"); $em->persist($ActivityComeFromService); $em->flush(); echo "Import de l'activité num ".$a." type :".$activity->type." effectué avec succès\n"; unset($ActivitySessionEnduranceUnderWater); unset($ActivityComeFromService); } echo "Après chaque activité : ".memory_get_usage()."\n"; unset($sport); unset($activityDetail); $em->clear(); } } } catch (\Exception $e) { throw $e; } } 

Спасибо, @AdrienBrault. Я тестировал с --env=prod --no-debug , и это правда, что он потребляет меньше памяти, но память все еще увеличивается. Как я могу очистить диспетчер объектов? и стабилизировать память?

Symfony регистрирует все SQL-запросы в среде dev, поэтому сначала вам нужно отключить его

 // disable logger $em->getConnection()->getConfiguration()->setSQLLogger(null); 

Вы можете использовать прослушиватели событий для сущностей, это также может увеличить использование памяти. Вы можете отключить их так

 // remove all listeners foreach ($em->getEventManager()->getListeners() as $event => $listeners) { foreach ($listeners as $listener) { $em->getEventManager()->removeEventListener($event, $listener); } } 

Удалите unset из вашего кода, в этом нет необходимости, поскольку вы очищаете диспетчер сущностей каждый шаг вашего цикла.

 // save and clear $em->flush(); $em->getUnitOfWork()->clear(); 

Помните, что доктрина может оптимизировать ваши запросы и улучшить производительность, если вы группируете запросы в один flush . Таким образом, наилучшей практикой было бы выполнить flush один раз над некоторыми частями ваших данных. Например:

 // collect 100 entities and then save them if (($i % 100) == 0) { $em->flush(); $em->getUnitOfWork()->clear(); } 

Попробуйте сбросить диспетчер объектов с помощью:

 $this->getContainer()->get('doctrine')->resetEntityManager(); 

а потом:

 $em = $this->getContainer()->get('doctrine')->getEntityManager();