コード例 #1
0
 /**
  * Creates a new Release model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Release();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
コード例 #2
0
 /**
  * Creates a new Release model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($artistId = null)
 {
     $release = new Release();
     if ($artistId) {
         $release->artist_id = $artistId;
     }
     $covers = [];
     $tracks = [];
     $artist_name = $artistId ? $release->artist->original_name : '';
     if (Yii::$app->request->isPost) {
         for ($i = 0; $i < count(Yii::$app->request->post('Cover', [])); $i++) {
             $covers[] = new Cover();
         }
         for ($i = 0; $i < count(Yii::$app->request->post('Track', [])); $i++) {
             $tracks[] = new Track();
         }
         $release->load(Yii::$app->request->post());
         $is_release_valid = $release->validate();
         if (!$release->artist_id) {
             $artist_name = Yii::$app->request->post()['artist_name'];
             $artist = new Artist(['original_name' => $artist_name, 'user_id' => Yii::$app->user->id]);
             $is_artist_valid = $artist->validate();
         } else {
             $is_artist_valid = true;
         }
         Cover::loadMultiple($covers, Yii::$app->request->post());
         $is_covers_valid = Cover::validateMultiple($covers);
         Track::loadMultiple($tracks, Yii::$app->request->post());
         $is_tracks_valid = Track::validateMultiple($tracks);
         if ($is_release_valid && $is_artist_valid && $is_covers_valid && $is_tracks_valid) {
             $transaction = Yii::$app->db->beginTransaction();
             try {
                 if (isset($artist)) {
                     $artist->save(false);
                     $release->artist_id = $artist->id;
                 }
                 $release->save(false);
                 foreach ($covers as $cover) {
                     $release->link('covers', $cover);
                 }
                 foreach ($tracks as $track) {
                     $release->link('tracks', $track);
                 }
                 $tags = Yii::$app->request->post('Release', '');
                 $tags = Tag::find()->where(['in', 'id', explode(',', $tags['tags'])])->all();
                 foreach ($tags as $tag) {
                     $release->link('tags', $tag);
                 }
                 $transaction->commit();
                 return $this->redirect(['view', 'id' => $release->id]);
             } catch (\Exception $e) {
                 $transaction->rollBack();
                 throw $e;
             }
         }
     }
     return $this->render('create', ['release' => $release, 'covers' => $covers, 'tracks' => $tracks, 'trackProto' => new Track(), 'artist_name' => $artist_name]);
 }