2017-02-16 290 views
1

我試圖將數據從我的表單保存到數據庫。但是當我點擊「提交」按鈕什麼都沒有發生(頁面刷新,但我的數據庫表爲空)我做錯了什麼? 我創建模型,擴展ActiveRecord的:將數據保存到數據庫yii2

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('[email protected]') 
       ->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; 
     } 
    } 
    } 

然後更新我的視圖文件顯示形式,查看它只是簡單的幾個字段和上傳文件按鈕(但所有信息不保存)

<?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() ?> 

然後我將該代碼添加到我的控制器。我創建了新動作ActionForm並將其放入該代碼中:

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

} 

回答

0

爲什麼你重新聲明數據庫中的變量?你基本上是在告訴yii忽略表中的屬性。

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

刪除公共聲明,看看它是否工作。

0

您的代碼看起來沒問題,所以可能會出現一些驗證錯誤。

在insertFormData()方法中添加如下得到驗證錯誤:

if (!$entryForm->validate()){ 
    var_dump($entryForm->getErrors()); 
} 

後來編輯:因爲$ entryForm->負荷加載從數據

你insertFormData方法根本沒用POST。 第二個問題可能是文件上傳。要獲取上傳的文件,請使用UploadedFile :: getInstance($ model,'imageFile')。更多信息here

我建議你使用Gii(crud generator)創建一個crud,然後根據上面提到的文檔實現文件上傳。在這種情況下,您也會看到驗證錯誤。

+0

我把你的代碼,但什麼都沒有發生 – Rosti

+0

看到更新的anwser –