public function actionCurrencyTest() { //for($i=0; $i<45; $i++) { echo $i = 0; $data_slash = Helper::getDateIntervalYesterdayInDashOrSlashFormat(new \DateTime(), $i, 'slash'); $data_dash = Helper::getDateIntervalYesterdayInDashOrSlashFormat(new \DateTime(), $i, 'dash'); $url = 'http://www.cbr.ru/scripts/XML_daily.asp?date_req=' . $data_slash; $xml_contents = file_get_contents($url); if ($xml_contents === false) { throw new \ErrorException('Error loading ' . $url); } $xml = new \SimpleXMLElement($xml_contents); //$date = $xml->attributes()->Date; foreach ($xml as $node) { $current_curr = new CurrHistory(); //var_dump($node->attributes()->ID); if (Currencies::find()->where('valute_id like "%' . $node->attributes()->ID . '%"')->one()) { // var_dump(Currencies::find()->where('valute_id like "%'.$node->attributes()->ID.'%"')->one()); $current_curr->currency_id = Currencies::find()->where('valute_id like "%' . $node->attributes()->ID . '%"')->one()->id; } else { $new_currency = new Currencies(); $new_currency->name = TranslateHelper::translit($node->Name); $new_currency->valute_id = $node->attributes()->ID; $new_currency->char_code = $node->CharCode; $new_currency->num_code = $node->NumCode; $new_currency->save(false); $current_curr->currency_id = $new_currency->id; //var_dump($new_currency); exit; } $current_curr->date = $data_dash; $current_curr->nominal = $node->Nominal; $current_curr->value = str_replace(',', '.', $node->Value); //замена запятой на точку позволят использовать //формат с плавоющей точкой $current_curr->save(false); //echo $current_curr->value; exit; } // } }
/** * @return \yii\db\ActiveQuery */ public function getCurrHistories() { return $this->hasMany(CurrHistory::className(), ['currency_id' => 'id']); }