public function getPropertyGroups($force = false, $getByObjectId = false, $createAbstractModel = false) { $message = "getPropertyGroups " . $this->owner->className() . ':' . $this->owner->id . " force:" . ($force ? 'true' : 'false') . " getByObjectId:" . ($getByObjectId ? 'true' : 'false') . " createAbstractModel:" . ($createAbstractModel ? 'true' : 'false'); Yii::trace($message, 'properties'); $cacheKey = $message; if ($this->properties === null && $force === false) { // try to get from cache $data = Yii::$app->cache->get($message); if ($data !== false) { Yii::trace("Properties from cache", 'properties'); $this->abstract_model = isset($data['abstract_model']) ? $data['abstract_model'] : []; $this->properties = $data['properties']; $this->property_key_to_id = $data['property_key_to_id']; $this->property_id_to_group_id = $data['property_id_to_group_id']; } } if ($createAbstractModel === true && is_null($this->abstract_model)) { // force creating abstract model if it is null and needed $force = true; } if ($this->properties === null || $force === true) { $tags = [ActiveRecordHelper::getObjectTag($this->owner->className(), $this->owner->id)]; $this->properties = []; if ($getByObjectId === true) { $groups = PropertyGroup::getForObjectId($this->getObject()->id); } else { $groups = PropertyGroup::getForModel($this->getObject()->id, $this->owner->id); } $values_for_abstract = []; $properties_models = []; $rules = []; /** @var PropertyGroup $group */ foreach ($groups as $group) { $tags[] = ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $group->id); $this->properties[$group->id] = []; $props = Property::getForGroupId($group->id); foreach ($props as $p) { $values = $this->getPropertyValues($p); $this->properties[$group->id][$p->key] = $values; if ($createAbstractModel === true) { $values_for_abstract[$p->key] = $values; } $properties_models[$p->key] = $p; $this->property_key_to_id[$p->key] = $p->id; if (!isset($this->property_id_to_group_id[$group->id])) { $this->property_id_to_group_id[$group->id] = [$p->key]; } else { $this->property_id_to_group_id[$group->id][] = $p->key; } if ($createAbstractModel === true) { $handlerAdditionalParams = Json::decode($p->handler_additional_params); if (isset($handlerAdditionalParams['rules']) && is_array($handlerAdditionalParams['rules'])) { foreach ($handlerAdditionalParams['rules'] as $rule) { if (is_array($rule)) { $rules[] = ArrayHelper::merge([$p->key], $rule); } else { $rules[] = [$p->key, $rule]; } } } } } } if ($createAbstractModel === true) { $this->abstract_model = new AbstractModel([], $this->owner); $this->abstract_model->setPropertiesModels($properties_models); $this->abstract_model->setAttributes($values_for_abstract); $this->abstract_model->setFormName('Properties_' . $this->owner->formName() . '_' . $this->owner->id); $this->abstract_model->addRules($rules); } $res = Yii::$app->cache->set($cacheKey, ['properties' => $this->properties, 'abstract_model' => $this->abstract_model, 'property_id_to_group_id' => $this->property_id_to_group_id, 'property_key_to_id' => $this->property_key_to_id], 86400, new TagDependency(['tags' => $tags])); Yii::trace('putting props to cache: ' . ($res ? 'true' : 'false') . ' key:' . $cacheKey, 'properties'); } return $this->properties; }