我喜欢清楚地解释我的问题, 我正在使用 wbraganca/yii2-dynamicform 这里
create actio
n 工作得很好,但是在 update
动作
在我标记的代码中,我不知道我需要做什么,我在
(addresses)
表中没有这样的字段customer
。我被困住了。
假设如果我在模型中创建一个像
public $addressess
这样的变量,它会让我再次重新加载表格,这会导致在更新相同的表单时,数据重新加载并且表单显示为空而不是空,
如果用这个名字创建一个函数,我不知道该写什么.. 我只是使用这样的代码
public function getaddressess()
{
}
创建操作代码
public function actionCreate()
{
$modelCustomer = new Customer;
$modelsAddress = [new Address];
if ($modelCustomer->load(Yii::$app->request->post())) {
$modelsAddress = Model::createMultiple(Address::classname());
Model::loadMultiple($modelsAddress, Yii::$app->request->post());
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsAddress),
ActiveForm::validate($modelCustomer)
);
}
// validate all models
$valid = $modelCustomer->validate();
$valid = Model::validateMultiple($modelsAddress) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $modelCustomer->save(false)) {
foreach ($modelsAddress as $modelAddress) {
$modelAddress->customer_id = $modelCustomer->id;
if (! ($flag = $modelAddress->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $modelCustomer->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('create', [
'modelCustomer' => $modelCustomer,
'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress
]);
}
帮我解决这个问题
$modelsAddress=$modelCustomer->addresses
表示相关 Address()
实例的数组
public function actionCreate()
{
$modelCustomer = new Customer;
$modelsAddress = $this->getaddressess($modelCustomer->id);
//...................
}
public function getaddressess($id)
{
$model = Address::find()->where(['id' => $id])->all();
return $model;
}
来自
public function getaddressess($id)
{
$model = Address::find()->where(['id' => $id])->all();
return $model;
}
上面分享的您还需要添加
在您的更新视图文件上:
'model' => $model,
'modelsAddress'=>$modelsAddress,
希望这有帮助。它对我有用
应该是
getAddresses()
而不是 getaddresses()
(虽然两者都可以工作,但我会选择第一个以满足惯例)。或者,如果不需要额外的封装,您可以设置 public $addresses
。
suppose if i create a variable in model like public $addressess, it makes me the reload the table again, and that cause while update the same form, data's getting reload and form viewing as empty without empty,
我认为您遇到了验证问题 - 没有验证器将该字段标记为安全,并且您在发布后将其视为空。
public $addresses
添加到您的客户模型中。这行代码 ---> $modelsAddress = $modelCustomer->addresses;
是从 model 获取 customer 的行 ---> public function getAddresses()
此公共函数行代码是在 yii2 上从活动记录方法获取数组相关表的代码。
$modelCustomer->addresses 单词地址应该来自 $modelCustomer 模型,您必须与添加多个值的另一个表有关系。在视频中描述的示例中,我有两个表 po 表和 po_items 表 po_items 表具有 po_id 外键。因此,当您使用 gii 制作模型时,您将在模型中获得必须使用的关系,而不是地址。
根据我的数据库的关系应该是 - poItems 你会在第 14 行看到这个
将此添加到客户模型
public function getAddresses(){
return $this->hasMany(Address::className(), ['id' => 'id']);
}
public function getPoItems()
{
return $this->hasMany(PoItem::className(), ['po_id' => 'id']);
}
在PoController.php中
public function actionUpdate($id)
{
$model = $this->findModel($id);
//$modelsPoItem = [new PoItem];
$modelsPoItem = $model->poItems;
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$oldIDs = ArrayHelper::map($modelsPoItem, 'id', 'id');
$modelsPoItem = Model::createMultiple(PoItem::classname(), $modelsPoItem);
Model::loadMultiple($modelsPoItem, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPoItem, 'id', 'id')));
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsPoItem) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs))
{
PoItem::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsPoItem as $modelPoItem)
{
$modelPoItem->po_id = $model->id;
if (! ($flag = $modelPoItem->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('update', [
'model' => $model,
'modelsPoItem' => (empty($modelsPoItem)) ? [new PoItem] : $modelsPoItem
]);
}