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