/**
  * Creates a new Collection model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Collection();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $userCollectionModel = new \common\models\UserCollection();
         $userCollectionModel->user_id = yii::$app->user->id;
         $userCollectionModel->collection_id = $model->id;
         $userCollectionModel->member_type_id = Types::$member_type['manager']['id'];
         $userCollectionModel->save();
         return $this->redirect(['/collections']);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Esempio n. 2
0
 /**
  * 保存或删除一个收藏
  * @param $userId
  * @param $testLibraryId
  * @return string
  * @throws Exception
  * @throws \Exception
  */
 public static function saveOrDelete($userId, $testLibraryId)
 {
     $collection = Collection::find()->where(['userId' => $userId, 'testLibraryId' => $testLibraryId])->one();
     if ($collection) {
         if (!$collection->delete()) {
             throw new Exception("Collection delete error");
         }
         return "delete";
     } else {
         $collection = new Collection();
         $collection->userId = $userId;
         $collection->testLibraryId = $testLibraryId;
         $collection->createDate = DateFunctions::getCurrentDate();
         if (!$collection->save()) {
             throw new Exception("Collection save error");
         }
         return "collected";
     }
 }