/** * Finds the ParseRegion model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ParseRegion the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ParseRegion::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionUpdateReport() { foreach (ParseRegion::find()->all() as $region) { /* @var $region ParseRegion*/ echo $region->url . "\n"; $saved = 0; $errors = []; $models = ParseKsk::find()->where(['parse_region_id' => $region->id])->andWhere(['IS NOT', 'url_otchet', null])->all(); foreach ($models as $model) { /* @var $model ParseKsk */ echo ' ' . $model->url_otchet . "\n"; $saw = new NokogiriHelper(file_get_contents($model->url_otchet)); $ok = $saved; foreach ($saw->get('.col_f_content') as $forum_name) { $h4s = $forum_name['h4']; if (isset($forum_name['span'][0]['span'])) { $posted_at = $forum_name['span'][0]['span'][0]['#text'][0]; } else { $posted_at = $forum_name['span'][1]['span'][0]['#text'][0]; } foreach ($h4s as $h4) { $a = $h4['a'][0]; $name = trim($a['span'][0]['#text'][0]); $url = $a['href']; $model_otchet = ParseOtchet::find()->where(['name' => $name])->one(); if (is_null($model_otchet)) { $model_otchet = new ParseOtchet(); $model_otchet->parse_ksk_id = $model->id; $model_otchet->name = $name; $model_otchet->url_otchet = $url; $model_otchet->posted_at = date('Y-m-d H:i:s', strtotime($posted_at)); $model_otchet->updated_at = new \yii\db\Expression('utc_timestamp()'); $model_otchet->save() ? $saved++ : ($errors[] = $model_otchet->errors); } } } if ($ok < $saved) { $model = ParseKsk::findOne($model->id); $model->updated_at = new \yii\db\Expression('utc_timestamp()'); $model->save(); } } if ($saved > 0) { $region = ParseRegion::findOne($region->id); $region->updated_at = new \yii\db\Expression('utc_timestamp()'); $region->save(); } echo "saved - {$saved}\n"; } }