/** * Initialize function. Creating Counters&LinkCounters records * * @param int $id */ public function initializeEstate($id) { /** @var Estate $estate */ $estate = $this->findEstate($id); $communalList = ComService::findByCity($estate->city_id); // Get list of all ComServices in Estate's city /** @var ComService $communal */ foreach ($communalList as $communal) { /** @var Counter $counter */ $counter = new Counter($communal->type); // Create new counter with specified type $counter->save(); $model = new LinkCounter(); // Create full LinkCounter record $model->setComserviceId($communal->id); $model->setEstateId($estate->id); $model->setCounterId($counter->id); $model->save(); } }