/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Listing::find()->innerJoin('ebay_account', 'listing.ebay_id = ebay_account.id')->where(['ebay_account.user_id' => Yii::$app->user->id]); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'ebay_id' => $this->ebay_id, 'price' => $this->price, 'qty' => $this->qty, 'sold_qty' => $this->sold_qty, 'sync_at' => $this->sync_at]); $query->andFilterWhere(['like', 'item_id', $this->item_id])->andFilterWhere(['like', 'sku', $this->sku])->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * Displays a single Product model. * @param integer $id * @return mixed */ public function actionView($id) { $product = $this->findModel($id); $listings = Listing::find()->where(['sku' => $product->sku, 'user_id' => Yii::$app->user->id])->indexBy('ebay_id')->all(); return $this->render('view', ['model' => $product, 'listings' => $listings]); }
/** * 同步一页 */ public function actionSyncPage() { if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; $post = Yii::$app->request->post(); //return $result['listing']=var_dump($post['pageNumber']); $ebayListing = new EbayListing($post['ebayID']); $activeListing = $ebayListing->getListingPage((int) $post['pageNumber']); if (!isset($activeListing['Error'])) { $result['status'] = 'success'; $result['message'] = 'Connection Success'; $result['listings_sync'] = []; $result['listings_nosku'] = []; foreach ($activeListing['listings'] as $listing) { if (isset($listing['sku'])) { $newListing = Listing::find()->where(['item_id' => $listing['item_id']])->one(); if ($newListing == null) { $newListing = Listing::find()->where(['ebay_id' => $post['ebayID'], 'sku' => $listing['sku']])->one(); if ($newListing != null) { $newListing->delete(); } $newListing = new Listing(); $newListing->item_id = $listing['item_id']; } $newListing->sku = $listing['sku']; $newListing->ebay_id = $post['ebayID']; $newListing->price = $listing['price']; $newListing->title = $listing['title']; $newListing->qty = $listing['qty']; $newListing->sold_qty = $listing['sold_qty']; $newListing->sync_at = date('Y-m-d H:i:s', time()); $newListing->user_id = Yii::$app->user->id; if ($newListing->save()) { $result['listings_sync'][] = $listing['item_id']; } else { $result['status'] = 'error'; $result['message'] .= "<br>" . $listing['item_id'] . " failed to sync"; $result['savingError'][] = $newListing->errors; } } else { //if no sku $newListing = Listing::find()->where(['item_id' => $listing['item_id']])->one(); if ($newListing !== null) { $newListing->delete(); } $result['listings_nosku'][] = $listing['item_id']; } } //$result['listings'] = $activeListing['listings']; } else { $result['status'] = 'error'; $result['message'] = 'Follow errors:' . "<br>"; foreach ($activeListing['Error'] as $error) { $result['message'] .= $error . "<br>"; } } return $result; } else { return false; } }