2016-04-21 35 views
0

我有一個表格製作3個字段productname,batchno,數量。我得到的數據產品名稱batchno來自產品批量模型。我可以清楚地插入它。現在我想插入帶有3個字段的瓶子名稱,產品名稱,數量的表格「bottle」。產品名稱與瓶子名稱有關係。數據來自模型產品名稱。我想在我選擇產品名稱時加載瓶子名稱。我可以填充數據並可以在螢火蟲中看到。我想要的是在生產表單中添加文本輸入並以相同的形式顯示瓶子名稱。添加一個不屬於yii2中相同模型的文本輸入

生產控制器

public function actionCreate() 
    { 
     $model = new Production(); 
     $productname = new Productnames(); 
     $bottle = new Bottle(); 

     if ($model->load(Yii::$app->request->post()) && $productname->load(Yii::$app->request->post())) 
     { 
      $model->save(); 
      //$bottle->attributes = $model->attributes; 
      $bottle->usedate = $model->productiondate; 
      $bottle->useqty = $model->prodqty; 
      $bottle->productname = $model->productname; 
      $bottle->bottlename = $productname->bottletype; 
      // $employee->emp_email = $model->emp_email; 
      // $employee->emp_mobile = $model->emp_mobile; 
      $bottle->save(); 
      return $this->redirect(['create']); 
     } else { 
      return $this->render('create', [ 
       'model' => $model, 
       'bottle' => $bottle, 
      ]); 
     } 
    } 

生產_form

<div class="production-form"> 


    <?php $form = ActiveForm::begin(); ?> 


    <!--<?= Html::a('Select Product', ['/production/productbatch/index'], ['class'=>'btn btn-primary']) ?> --> 

    <?= $form->field($model, 'productiondate')->widget(
    DatePicker::className(), [ 
     // inline too, not bad 
     'inline' => false, 
     // modify template for custom rendering 
     //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>', 
     'clientOptions' => [ 
      'autoclose' => true, 
      'format' => 'yyyy-mm-dd' 
     ] 
    ]);?> 

    <!-- echo CHtml::button("(+)",array('title'=>"Select Product",'onclick'=>'js:selectproductforproduction();')); --> 

    <?= $form->field($model, 'productname')->widget(Select2::classname(), [ 
    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'), 
    'language' => 'en', 
    'options' => ['placeholder' => 'Select Product Name', 'id' => 'catid'], 
    'pluginOptions' => [ 
     'allowClear' => true 
    ], 
    ]); ?> 

    <?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [ 
    'options'=>['id'=>'subcat-id'], 
    'pluginOptions'=>[ 
     'depends'=>['catid'], 
     'placeholder'=>'Select BatchNo', 
     'url'=>Url::to(['/production/productbatch/subcat']) 
    ] 
    ]); ?> 

    <?= $form->field($model, 'prodqty')->textInput() ?> 

    <?= $form->field($productname, 'bottlename')->textInput() ?> 




    <div class="form-group"> 
     <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
    </div> 

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

</div> 

我得到的錯誤是 - Here

Productnames型號 -

<?php 

namespace frontend\modules\production\models; 

use Yii; 

/** 
* This is the model class for table "productnames". 
* 
* @property integer $productid 
* @property string $company 
* @property string $type 
* @property string $productnames_productname 
* @property integer $inventory 
* @property string $unitrmcost 
* @property string $unitlabelcost 
* @property string $unitcartonecost 
* @property string $productnames_labelname 
* @property string $productnames_cartonename 
* @property string $bottletype 
* @property string $captype 
* 
* @property Productbatch[] $productbatches 
* @property Production[] $productions 
* @property Bottlename $bottletype0 
* @property Capname $captype0 
* @property Cartonename $productnamesCartonename 
* @property Labelname $productnamesLabelname 
* @property Productsales[] $productsales 
*/ 
class Productnames extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'productnames'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['company', 'type', 'productnames_productname'], 'required'], 
      [['inventory'], 'integer'], 
      [['company'], 'string', 'max' => 40], 
      [['type', 'unitrmcost', 'unitlabelcost', 'unitcartonecost'], 'string', 'max' => 10], 
      [['productnames_productname', 'productnames_labelname', 'productnames_cartonename'], 'string', 'max' => 60], 
      [['bottletype', 'captype'], 'string', 'max' => 50], 
      [['productnames_productname'], 'unique'] 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'productid' => 'Productid', 
      'company' => 'Company', 
      'type' => 'Type', 
      'productnames_productname' => 'Productnames Productname', 
      'inventory' => 'Inventory', 
      'unitrmcost' => 'Unitrmcost', 
      'unitlabelcost' => 'Unitlabelcost', 
      'unitcartonecost' => 'Unitcartonecost', 
      'productnames_labelname' => 'Productnames Labelname', 
      'productnames_cartonename' => 'Productnames Cartonename', 
      'bottletype' => 'Bottletype', 
      'captype' => 'Captype', 
     ]; 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getProductbatches() 
    { 
     return $this->hasMany(Productbatch::className(), ['productname' => 'productnames_productname']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getProductions() 
    { 
     return $this->hasMany(Production::className(), ['productname' => 'productnames_productname']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getBottletype0() 
    { 
     return $this->hasOne(Bottlename::className(), ['bottlename' => 'bottletype']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getCaptype0() 
    { 
     return $this->hasOne(Capname::className(), ['capname' => 'captype']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getProductnamesCartonename() 
    { 
     return $this->hasOne(Cartonename::className(), ['cartone_name' => 'productnames_cartonename']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getProductnamesLabelname() 
    { 
     return $this->hasOne(Labelname::className(), ['label_name' => 'productnames_labelname']); 
    } 

    /** 
    * @return \yii\db\ActiveQuery 
    */ 
    public function getProductsales() 
    { 
     return $this->hasMany(Productsales::className(), ['productname' => 'productnames_productname']); 
    } 
} 
+0

顯示相關controllerAction和查看 – scaisEdge

+0

我已更新問題。請看一看。 – Tanmay

+0

你可以顯示你的'Productnames'模型​​代碼嗎? – GAMITG

回答

1

你忘了渲染productname來創建視圖,所以通過它。

 return $this->render('create', [ 
      'model' => $model, 
      'bottle' => $bottle, 
      'productname' => $productname, 
     ]); 
+0

我在渲染中添加了'productname'=> $ productname。但仍然是同樣的錯誤。我已將您的產品名稱 – Tanmay

+0

添加到'Production _form'視圖中? – GAMITG

+0

我已經在問題中給出了生產_form。 – Tanmay

0

你必須使用$model = new Production();鑑於..所以你必須在生產類.like--

public function attributeLabels() 
{ 
    return [   
     'productname' => 'Product Name', 
    ]; 
} 

然後檢查它來定義產品名稱......

相關問題