I am implementing a routine where in need update or insert many registers.. But after many Model::findFirst I got the error Allowed memory size of 134217728 bytes exhausted...
There are a better way to resolve this?
Follow the code:
protected function categoryMacs($establishment, $macs, $categoryId){
$memoryLimit = ini_get("memory_limit");
foreach ($macs as $mac => $data) {
$this->logger->log("M: " . $this->formatBytes(memory_get_usage()) . " de " . $memoryLimit);
$establishmentCM = EstablishmentCategoryMac::findFirst("establishment_id = " . $establishment["id"] . " and mac = " . $mac);
if (! $establishmentCM) {
$establishmentCM = new EstablishmentCategoryMac();
$establishmentCM->setEstablishment_id($establishment["id"]);
$establishmentCM->setMac($mac);
}
$establishmentCM->setCategory_mac_id($categoryId);
$establishmentCM->save();
unset($establishmentCM);
}
}
LOG:
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.25 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.26 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.28 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.29 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.31 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.32 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.34 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.35 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.37 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.38 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.39 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.41 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.42 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.44 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.45 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.47 mb de 128M
- [Wed, 14 Dec 16 11:14:40 -0200][DEBUG] M: 51.48 mb de 128M