2015-09-16 5 views
5

में डेटाबेस में नहीं डाला जा सकता है मुझे नहीं पता कि क्या गलत है लेकिन मैं डेटाबेस में सम्मिलित नहीं कर सकता।ActiveRecord Yii2

यह मॉडल में मिला है।

warranty.php

<?php 

    namespace app\models; 

    use Yii; 
    use yii\db\ActiveRecord; 
    use yii\behaviors\TimestampBehavior; 
    use yii\db\Expression; 
    use yii\helpers\VarDumper; 
    /** 
    * This is the model class for table "warrantytwo". 
    * 
    * @property integer $id 
    * @property string $item_no 
    * @property string $warranty_date 
    * @property string $date 
    * @property integer $created_by 
    * @property string $tstamp 
    */ 
    class Warrantytwo extends ActiveRecord 
    { 
     /** 
     * @inheritdoc 
     */ 
     public static function tableName() 
     { 
      return 'warrantytwo'; 
     } 

     public function behaviors() 
     { 
      return [ 
       [ 
        'class' => TimestampBehavior::className(), 
        'createdAtAttribute' => 'tstamp', 
        'updatedAtAttribute' => false, 
        'value' => new Expression('NOW()'), 
       ], 
      ]; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function rules() 
     { 
      return [ 
       [['item_no', 'warranty_date'], 'required'], 

       [['warranty_date', 'date'], 'string'], 

       ['created_by', 'default', 'value' => 'none'], 

       [['created_by'], 'integer'], 

       [['warranty_date', 'date','tstamp'], 'safe'], 

       [['item_no'], 'string', 'max' => 100] 
      ]; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function attributeLabels() 
     { 
      return [ 
       'id' => 'ID', 
       'item_no' => 'Item No.', 
       'warranty_date' => 'Warranty Date', 
       'date' => 'Date', 
       'created_by' => 'Created By', 
       'tstamp' => 'tstamp', 
      ]; 
     } 

     public function addWarrantytwo() 
     { 
      if ($this->validate()) { 
       $Warrantytwo = new Warrantytwo(); 
       $Warrantytwo->item_no = $this->item_no; 
       $Warrantytwo->warranty_date = $this->warranty_date; 
       $Warrantytwo->created_by = 'admin1'; 
       $Warrantytwo->date = date('Y-m-d H:i:s'); 
       //$Warrantytwo->touch('tstamp'); 

       if ($Warrantytwo->save()) 
       { 
        return $Warrantytwo; 
       }else { 
        VarDumper::dump($Warrantytwo->getErrors()); 
       } 
      } 

      //return null; 
     } 
    } 

controller.php

public function actionWarranty() 
    { 
     //$model= new WarrantyDate(); 
     $model= new Warrantytwo(); 

     if ($model->load(Yii::$app->request->post()) && $model->addWarrantytwo()) { 
      return $this->redirect(['warranty']); 
     } 
     else { 
     return $this->render('warranty', [ 
      'model' => $model, 
      ]); 
     } 
    } 

और मेरे विचार में/प्रपत्र

<?php $form = ActiveForm::begin([ 
     'id' => 'new-warranty-form', 
     //'options' => ['class' => 'form-horizontal'], //for yii\ActiveForm 
     'type' => ActiveForm::TYPE_HORIZONTAL, //for krajee\ActiveForm 
     'formConfig' => ['labelSpan' => 4, 'deviceSize' => ActiveForm::SIZE_SMALL] ]); ?> 
       <div class="form-horizontal"> 

    <?= $form->field($model, 'item_no', 
     ['addon' => ['append' => ['content'=> '<a href="#w" onClick="sample()"><i class="glyphicon glyphicon-search"></i></a>']] ]); ?>       

    <?= $form->field($model, 'warranty_date')-> widget(DatePicker::classname(), 
    [ 
    'options' => ['placeholder' => 'Date Claimed'], 
    'pluginOptions' => [ 'autoclose'=>true ] 
       ]); ?> 

    </div> 
    <div class="col-sm-offset-5 col-md-9"> 

    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'warranty-button']) ?> 

    <?= Html::resetButton('Reset', ['class' => 'btn btn-default']); ?> 
    </div> 

    <?php ActiveForm::end(); ?> 

जब मैं उस पर जाँच यह किसी भी त्रुटि नहीं देता है लेकिन डेटाबेस, कुछ भी जोड़ा नहीं। मुझे उम्मीद है कि कोई भी मेरी मदद कर सकता है। धन्यवाद। मैं YII2 के लिए नया हूं इसलिए मुझे अभी भी यह नहीं मिला है।

+0

क्या आप मुझे कार्रवाई के लिए 'warranty_date' पोस्ट का मूल्य बता सकते हैं? – GAMITG

+0

@gamitg i मान एक तारीख है, मुझे लगता है। मैंने एक कार्टिक डेटपिकर विजेट का इस्तेमाल किया। और मैंने यह सुनिश्चित करने के लिए कि तारीख को dbase में जोड़ा जा सकता है, यह सुनिश्चित करने के लिए मैंने 'date' के बजाय mysql में 'text' में अपना डेटा प्रकार भी बदल दिया है। – BlackSkull

+0

आप '$ वारंटीटवो-> सेव (झूठी) 'का उपयोग कर डेटा को सहेजने का प्रयास कर सकते हैं। इसका मतलब यह है कि समस्या को हल करने के लिए सत्यापन को डेटा के बिना सहेजने के लिए। – GAMITG

उत्तर

2

चूंकि आपको यह निर्धारित करना होगा कि इस स्थिति में समस्या किस स्थिति में होती है। (कभी-कभी यह थोड़ा भ्रम पैदा करता है क्योंकि वैध() हमेशा कुछ देता है)

public function addWarrantytwo() 
    { 
     if ($this->validate()) { 
      $Warrantytwo = new Warrantytwo(); 
      $Warrantytwo->item_no = $this->item_no; 
      $Warrantytwo->warranty_date = $this->warranty_date; 
      $Warrantytwo->created_by = 'admin1'; 
      $Warrantytwo->date = date('Y-m-d H:i:s'); 
      //$Warrantytwo->touch('tstamp'); 

      if ($Warrantytwo->save(false)) // try save without validation 
      { 
       return $Warrantytwo; 
      }else { 
       VarDumper::dump($Warrantytwo->getErrors()); 
       exit; 
      } 
     } 
     else { 
      VarDumper::dump($this->validate()); // check for the correct flow 
      exit; 
     } 

     //return null; 
    } 
+0

हैलो, महोदय। फॉर्म सबमिट करने के बाद मैंने आपके जवाब की कोशिश की, मुझे केवल "झूठा" शब्द मिलता है। आपको लगता है कि त्रुटि कहां थी? जवाब के लिए धन्यवाद। :) – BlackSkull

+1

फिर मान्य वापसी झूठी। अपने नियमों को चुनिंदा रूप से टिप्पणी करने के लिए एआरआर आरटीई खोजने के लिए। इससे आपको सत्यापन त्रुटि के कारण की पहचान करने में मदद मिलेगी। '[['warranty_date', 'date'], 'string'], 'दिनांक स्ट्रिंग अजीब है। – scaisEdge

+0

मुझे लगता है कि मुझे पता है कि त्रुटि कहां है। यह बनाया_by का डेटा प्रकार है। लेकिन मैं आपके एवरवर को वोट दूंगा क्योंकि मुझे यह पता लगाने में मदद मिलती है कि त्रुटि कहां हुई। धन्यवाद! – BlackSkull