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());
         }
     }
 }
Пример #2
0
 public function actionForm()
 {
     //vd(1);
     $model = Profile::find()->where(['user_id' => Yii::$app->user->id])->one();
     if (!$model) {
         $model = new Profile();
     }
     if ($model->load(Yii::$app->request->post())) {
         //vd($_POST);
         if ($model->validate()) {
             $model->gender = $_POST['gender'];
             $model->user_id = Yii::$app->user->id;
             $model->login = User::getLoginById(Yii::$app->user->id);
             $model->save();
             return $this->render('form', ['model' => $model]);
         } else {
             vd($model->getErrors());
         }
     }
     return $this->render('form', ['model' => $model]);
 }