public function actionSettings() { $user = Yii::$app->user->getIdentity(); // Update/Render if exist if (isset($user)) { $modelAttributes = Yii::$app->request->post('ModelAttribute'); $count = count($modelAttributes); $attributes = []; for ($i = 0; $i < $count; $i++) { $attribute = new ModelAttribute(['parentId' => $user->id, 'parentType' => CoreGlobal::TYPE_USER]); $attributes[] = $attribute; } // Load SchoolItem models if (ModelAttribute::loadMultiple($attributes, Yii::$app->request->post(), 'ModelAttribute') && ModelAttribute::validateMultiple($attributes)) { UserService::updateAttributes($user, $attributes); $data = []; foreach ($attributes as $attribute) { $data[] = $attribute->getFieldInfo(); } // Trigger Ajax Success return AjaxUtil::generateSuccess(Yii::$app->cmgCoreMessage->getMessage(CoreGlobal::MESSAGE_REQUEST), $data); } // Trigger Ajax Failure return AjaxUtil::generateFailure(Yii::$app->cmgCoreMessage->getMessage(CoreGlobal::ERROR_REQUEST)); } // Model not found return AjaxUtil::generateFailure(Yii::$app->cmgCoreMessage->getMessage(CoreGlobal::ERROR_REQUEST), ['session' => true]); }
public static function update($attribute) { $existingAttribute = ModelAttribute::findByTypeName($attribute->parentId, $attribute->parentType, $attribute->type, $attribute->name); if (isset($existingAttribute)) { $existingAttribute->copyForUpdateFrom($attribute, ['value']); $existingAttribute->update(); return $existingAttribute; } else { $attribute->save(); return $attribute; } }
/** * @return ModelAttribute - associated with parent */ public function getModelAttributeByTypeName($type, $name) { $parentType = $this->attributeType; return $this->hasMany(ModelAttribute::className(), ['parentId' => 'id'])->where("parentType=:ptype AND type=:type AND name=:name", [':ptype' => $parentType, ':type' => $type, ':name' => $name])->one(); }