public function up() { $data = (include Yii::getAlias('@common') . '/data/test-adverts.php'); $i = 0; while ($i++ !== 3) { foreach ($data as $one) { $advert = new Advert(); $advert->detachBehavior('timestamp'); $advert->setAttributes($one); $advert->user_id = rand(1, 50); $advert->created_at = time() - 3600 * 24 * rand(1, 31) - 3600 * rand(1, 24) + rand(1, 3600); $advert->updated_at = $advert->created_at; $advert->term_at = $advert->created_at + 3600 * 24 * rand(1, 31); if ($advert->save()) { echo "Advert for user №{$advert->user_id} created\n"; } else { print_r($advert->getErrors()); } } } foreach (User::find()->all() as $user) { $profile = new Profile(); $profile->user_id = $user->id; $profile->name = $user->username; if ($profile->save()) { echo "Profile of \"{$user->username}\" created\n"; } else { print_r($profile->getErrors()); } } }
/** * Creates a new Advert model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Advert(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['step2']); } else { return $this->render('create', ['model' => $model]); } }
/** * Test action. */ public function actionVk() { $wall = file_get_contents("https://api.vk.com/method/wall.get?owner_id=-120504421&filter=others"); $wall = json_decode($wall); foreach ($wall->response as $item) { if (is_object($item)) { $advert = new Advert(); $advert->setScenario(Advert::SCENARIO_CREATE_FROM_SERVICE); $advert->content = $item->text; $advert->created_at = $item->date; $advert->save(); if (isset($item->attachments)) { foreach ($item->attachments as $a) { $type = $a->type; $f = $a->{$type}; $file = new \common\models\Image(); $file->remote_url = $f->src_big; $file->created_at = $f->created; $file->save(); $file->attachOwner($advert->id, 'Advert', 'files'); } } } } }