/** * Creates a new Meme model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Meme(); try { if ($model->load($_POST) && $model->save()) { $post = Yii::$app->request->post(); $postOriginMemeVidmage = !empty($post['memeVidmageOrigin']) ? [$post['memeVidmageOrigin']] : []; $postMemeVidmages = !empty($post['memeVidmages']) ? $post['memeVidmages'] : []; $model->saveManyVidmages($postOriginMemeVidmage, true); $model->saveManyVidmages($postMemeVidmages); //return $this->redirect(Url::previous()); return $this->redirect(['update', 'id' => $model->id]); } elseif (!\Yii::$app->request->isPost) { $model->load($_GET); } } catch (\Exception $e) { $msg = isset($e->errorInfo[2]) ? $e->errorInfo[2] : $e->getMessage(); $model->addError('_exception', $msg); } return $this->render('create', ['model' => $model]); }
public function saveManyMemes($vidmageMemes) { foreach ($vidmageMemes as $vidmageMemeId) { if (!is_numeric($vidmageMemeId)) { $meme = new Meme(); $meme->name = $vidmageMemeId; $meme->save(); $vidmageMemeId = $meme->id; } $vidmageMeme = new MemeVidmage(); $vidmageMeme->vidmage_id = $this->id; $vidmageMeme->meme_id = $vidmageMemeId; $vidmageMeme->save(); } }