private function saveResult($result) { foreach ($result as $key => $res) { if ($key == 'elements') { foreach ($res as $data) { $product = LetualProduct::findOne(['article' => $data['article']]); if (!$product) { $product = new LetualProduct(); } $product->article = $data['article']; $product->title = $data['title']; $product->description = $data['description']; $letualPrice = new LetualPrice(); $letualPrice->article = $data['article']; $letualPrice->new_price = $this->getPrice($data['price']['newPrice']); $letualPrice->old_price = $this->getPrice($data['price']['oldPrice']); if (!empty($product)) { $product->brand = $this->clearBrand($result['brand']); $product->image_link = !empty($result['image']) ? $result['image'] : $product->image_link; $product->link = $result['link']; $product->group = $result['group']; $product->category = $result['category']; $product->sub_category = $result['sub_category']; $product->new_price = $letualPrice->new_price; $product->old_price = $letualPrice->old_price; $product->save(); if (!empty($letualPrice)) { $letualPrice->save(); } } } } } }
/** * @param Response $result */ private function saveLetualResult(Response $result) { $product = LetualProduct::findOne(['article' => $result->getArticle()]); if (!$product) { $product = new LetualProduct(); } $product->attributes = $result->toArray(); $product->new_price = $result->getNewPrice(); $product->old_price = $result->getPrice(); try { $lPrice = new LetualPrice(); $lPrice->article = $result->getArticle(); if ($result->getPrice()) { $lPrice->old_price = $result->getPrice(); } if ($result->getNewPrice()) { $lPrice->new_price = $result->getNewPrice(); } $product->deleted_at = '0000-00-00 00:00:00'; if ($product->save()) { if (!empty($lPrice->new_price) || !empty($lPrice->new_price)) { $lPrice->save(); } } else { \Yii::error(sprintf('Ошибка сохранения артикула L: %s data: %s', $result->getArticle(), json_encode($result->toArray())), 'cron'); } } catch (\Exception $e) { \Yii::error(sprintf('Exception %s сохранения артикула L %s data: %s', $e->getMessage(), $result->getArticle(), json_encode($result->toArray())), 'cron'); } }
/** * @return \yii\db\ActiveQuery */ public function getLetualPrices() { return $this->hasMany(LetualPrice::className(), ['article' => 'article']); }