public function increaseBuildingCounter($buildingId, $inc = 1) { $this->ensureBuildingCountersLoaded(); $buildingsOnBase = BuildingsOnBase::findOne(['base_id' => $this->id, 'building_id' => $buildingId]); if ($buildingsOnBase instanceof BuildingsOnBase) { $buildingsOnBase->updateCounters(['buildings_count' => $inc]); } else { $buildingsOnBase = new BuildingsOnBase(); $buildingsOnBase->base_id = $this->id; $buildingsOnBase->buildings_count = $inc; $buildingsOnBase->building_id = $buildingId; $buildingsOnBase->save(); } if (array_key_exists($buildingId, $this->buildingCounters)) { $this->buildingCounters[$buildingId] += $inc; } else { $this->buildingCounters[$buildingId] = $inc; } }
/** * @return \yii\db\ActiveQuery */ public function getBuildingsOnBases() { return $this->hasMany(BuildingsOnBase::className(), ['building_id' => 'id']); }