我有一个包含3个字段productname,batchno,qty的表格。我得到的数据productname,batchno来自productbatch模型。我可以清楚地插入它。现在我想插入表格" bottle"有3个字段的瓶名,产品名,数量。 Productname与bottlename有关。数据来自模型产品名称。我想在选择productname时加载瓶名。我可以填充数据,可以在firebug中看到。我想要的是在生产表单中添加textinput并以相同的形式显示瓶名。
生产控制器
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>
产品名称模型 -
<?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 :(得分:1)
这是变种。您可以使用两个模型实例(当前和相关)并将它们传递给查看http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.html或将虚拟属性添加到当前模型http://www.yiiframework.com/doc-2.0/guide-input-forms.html。
答案 1 :(得分:1)
您忘记渲染productname
来创建视图,因此请将其传递。
return $this->render('create', [
'model' => $model,
'bottle' => $bottle,
'productname' => $productname,
]);
答案 2 :(得分:0)
你在视图中使用$model = new Production();
..所以你必须在生产类中定义productname .like -
public function attributeLabels()
{
return [
'productname' => 'Product Name',
];
}
然后检查......