douci1615 2017-02-16 14:03
浏览 44
已采纳

将数据保存到db yii2

I'm trying to save data from my form to db. But when I click "submit" button nothing happened(page is refresh but my db table is blank) what I did wrong? I'm create model which extend ActiveRecord:

class EntryForm extends \yii\db\ActiveRecord
{
    public $id;
    public $name;
    public $email;
    public $age;
    public $height;
    public $weight;
    public $city;
    public $checkboxList;
    public $checkboxList1;
    public $imageFiles;
    public function rules()
    {
        return [
            [['name', 'email','age','height','weight','city','checkboxList','checkboxList1'], 'required'],
            [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg','maxFiles' => 5],
            ['email', 'email'],
        ];
    }
    public static function tableName()
    {
    return 'form';
    }
    public function attributeLabels()
   {
       return [
           'id' => 'ID',
           'name' => 'name',
           'email' => 'e-mail',
           'age' => 'age',
           'height' => 'height',
           'weight' => 'weight',
           'city' => 'city',
           'checkboxList' => 'technies',
           'checkboxList1' => 'english_level',
           'imageFiles[0]' => 'photo_1',
           'imageFiles[1]' => 'photo_2',
           'imageFiles[2]' => 'photo_3',
           'imageFiles[3]' => 'photo_4',
           'imageFiles[4]' => 'photo_5'
       ];
   }
   public function insertFormData()
   {
       $entryForm = new EntryForm();
       $entryForm->name = $this->name;
       $entryForm->email = $this->email;
       $entryForm->age = $this->age;
       $entryForm->height = $this->height;
       $entryForm->weight = $this->weight;
       $entryForm->city = $this->city;
       $entryForm->checkboxList = $this->checkboxList;
       $entryForm->checkboxList1 = $this->checkboxList1;
       $entryForm->imageFiles = $this->imageFiles;
       return $form->save();
   }
   public function contact($email)
  {
      if ($this->validate()) {
          Yii::$app->mailer->compose()
              ->setTo($email)
              ->setFrom('prozrostl@gmail.com')
              ->setSubject('Email from test app')
              ->setTextBody($this->name + $this->age + $this->height + $this->width + $this->city + $this->checkboxList + $this->checkboxList1 + $this->imageFiles)
              ->send();

          return true;
      } else {
          return false;
      }
  }
    }

then I update my view file to show the form, view it's just easy few fields and upload files button(but all information doesn't save)

<?php $form = ActiveForm::begin([
        'id' => 'my-form',
        'options' => ['enctype' => 'multipart/form-data']
    ]); ?>
    <div class="row">
        <div class="col-lg-6">
            <?= $form->field($entryForm, 'name')->textInput(['class'=>'name_class'])->input('name',['placeholder' => "Имя"])->label(false); ?>
        </div>
        <div class="col-lg-6">
            <?= $form->field($entryForm, 'email')->textInput()->input('email',['placeholder' => "E-mail"])->label(false); ?>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-6">
            <?= $form->field($entryForm, 'age')->textInput()->input('age',['placeholder' => "Возраст(полных лет)"])->label(false); ?>
        </div>
        <div class="col-lg-6">
            <?= $form->field($entryForm, 'height')->textInput()->input('height',['placeholder' => "Рост"])->label(false); ?>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-6">
            <?= $form->field($entryForm, 'weight')->textInput()->input('weight',['placeholder' => "Вес"])->label(false); ?>
        </div>
        <div class="col-lg-6">
            <?= $form->field($entryForm, 'city')->textInput()->input('city',['placeholder' => "Город проживания"])->label(false); ?>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-3">
            <p><img class="describe_images" src="computer.png"></img>Нужна ли техника в аренду</p>
        </div>
        <?= $form->field($entryForm, 'checkboxList')->checkboxList(['no'=>'Нет', 'yes_camera'=>'Да,только камера', 'yes_both'=>'да,компьютер и камера'])->label(false) ?>
    </div>
    <div class="row">
        <div class="col-lg-3">
            <p><img class="describe_images" src="English.png"></img>Знание английского</p>
        </div>

        <?= $form->field($entryForm, 'checkboxList1')->checkboxList(['starter'=>'Без знания', 'elementary'=>'Базовый', 'intermediate'=>'Средний','up-intermediate'=>'Высокий','advanced'=>'Превосходный'])->label(false) ?>
    </div>
<div class="row">
        <div class="col-lg-6">
<div class="col-lg-6">
            <p class="add_photo"><img class="describe_images" src="photo.png"></img>Добавить фото(до 5 штук)</p>

   </div>
   <div class="col-lg-6">
 <?= $form->field($entryForm, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*','id'=>'gallery-photo-add'])->label(false) ?>
   </div>


        </div>

        <div class="col-lg-6 pixels-line">
            <div class="preview"></div>
        </div>
    </div>
    <div class="form-group">
        <?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end() ?>

and then I add that code to my controller. I created new action ActionForm and put into that code:

public function actionForm()
{
    $entryForm = new EntryForm();
     if ($entryForm->load(Yii::$app->request->post()) && $entryForm->insertFormData()) {
     }

}
  • 写回答

2条回答 默认 最新

  • douchaqi3369 2017-02-18 11:49
    关注

    Why do you redeclare the variables in the database? you're basically telling yii to ignore the attributes on the table.

    public $id;
    public $name;
    public $email;
    public $age;
    public $height;
    public $weight;
    public $city;
    public $checkboxList;
    public $checkboxList1;
    public $imageFiles;
    

    Remove the public declarations and see if it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题