0], ['type', 'in', 'range' => [Favorite::FAVORITE_GOODS, Favorite::FAVORITE_SHOP, Favorite::FAVORITE_WORKER]], // [['id'], 'required',], ]; } public function save() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if (intval($this->type) === Favorite::FAVORITE_WORKER) { $existFavorite = Favorite::findOne([ 'store_id' => $this->store_id, 'user_id' => $this->user_id, 'type' => $this->type, 'worker_id' => $this->worker_id ]); } else { $existFavorite = Favorite::findOne([ 'store_id' => $this->store_id, 'user_id' => $this->user_id, 'goods_id' => $this->id, 'type' => $this->type, 'worker_id' => $this->worker_id ]); } if ($existFavorite) { $existFavorite->is_delete = $existFavorite->is_delete ? 0 : 1; $existFavorite->save(); return [ 'code' => 0, 'msg' => 'success', ]; } $favorite = new Favorite(); $favorite->store_id = $this->store_id; $favorite->user_id = $this->user_id; $favorite->goods_id = $this->id; $favorite->type = $this->type; $favorite->created_at = time(); $favorite->worker_id = $this->worker_id; if ($favorite->save()) { return [ 'code' => 0, 'msg' => 'success', ]; } else { return [ 'code' => 1, 'msg' => 'fail', ]; } } }