protected function processGear($key) { if ($this->isTest || !$this->gears instanceof PostGearsForm) { return null; } $gearForm = $this->gears->{$key}; if (!$gearForm instanceof BaseGearForm) { return null; } $gearModel = $gearForm->getGearModel(); // may null $primaryAbility = $gearForm->primary_ability ? Ability::findOne(['key' => $gearForm->primary_ability]) : null; $secondaryAbilityIdList = []; if (is_array($gearForm->secondary_abilities)) { foreach ($gearForm->secondary_abilities as $aKey) { if ($aKey == '') { $secondaryAbilityIdList[] = null; } else { if ($a = Ability::findOne(['key' => $aKey])) { $secondaryAbilityIdList[] = $a->id; } } } } $fingerPrint = GearConfiguration::generateFingerPrint($gearModel ? $gearModel->id : null, $primaryAbility ? $primaryAbility->id : null, $secondaryAbilityIdList); $config = GearConfiguration::findOne(['finger_print' => $fingerPrint]); if (!$config) { $config = new GearConfiguration(); $config->finger_print = $fingerPrint; $config->gear_id = $gearModel ? $gearModel->id : null; $config->primary_ability_id = $primaryAbility ? $primaryAbility->id : null; if (!$config->save()) { throw new \Exception('Could not save gear_counfiguration'); } foreach ($secondaryAbilityIdList as $aId) { $sub = new GearConfigurationSecondary(); $sub->config_id = $config->id; $sub->ability_id = $aId; if (!$sub->save()) { throw new \Exception('Could not save gear_configuration_secondary'); } } } return $config->id; }
/** * @return \yii\db\ActiveQuery */ public function getSecondaries() { return $this->hasMany(GearConfigurationSecondary::className(), ['config_id' => 'id']); }