Yii2动态表单更新操作不起作用

Yii2动态表单更新操作不起作用,yii2,Yii2,我喜欢把我的问题解释清楚, 我正在使用 这里的create action工作正常,但在updateaction中 在我标记的代码中,我不知道我需要做什么,我在customer表中没有这样的字段(地址)。我被这件事缠住了 假设我在模型中创建了一个变量,比如public$address,它会让我再次重新加载表,这会导致在更新同一表单时,数据被重新加载,表单被视为空而不是空 如果在该名称上创建函数,我不知道在该名称上写什么。。 我只是使用这样的代码 public function getaddress

我喜欢把我的问题解释清楚, 我正在使用 这里的
create actio
n工作正常,但在
update
action中

在我标记的代码中,我不知道我需要做什么,我在
customer
表中没有这样的字段
(地址)
。我被这件事缠住了

假设我在模型中创建了一个变量,比如
public$address
,它会让我再次重新加载表,这会导致在更新同一表单时,数据被重新加载,表单被视为空而不是空

如果在该名称上创建函数,我不知道在该名称上写什么。。 我只是使用这样的代码

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
        ]);
    }

请帮助我解决这个问题

它应该是
getAddresses()
而不是
getAddresses()
(虽然两者都可以工作,但我会选择第一个满足约定的)。如果不需要额外的封装,也可以设置
公共$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,
我认为您有一个验证问题-没有验证器将该字段标记为安全字段,并且您在发布后将其视为空字段

  • 公共$addresses
    添加到您的客户模型中
  • 将“地址”添加到安全的(或更合适的验证器)中。这样,在发布表单后,它可能不会呈现为空
  • 该行代码-->$modelsAddress=$modelCustomer->address

    是从第-->行的模型中为客户获取的


    此公共函数行代码是从yii2上的活动记录方法获取数组相关表的代码。

    $modelCustomer->地址单词地址应来自$modelCustomer模型。您必须与添加多个值的其他表有关系。在视频中描述的示例中,我有两个表po table和po_items table po_items table具有po_id的外键。因此,当您使用gii制作模型时,您将在模型中获得一个关系,该关系是您必须使用的,而不是地址


    根据我的数据库的关系应该是-poItems,您将在第14行看到这一点,在该示例中,表示相关
    地址()的数组

     public function actionCreate()
            {
                $modelCustomer = new Customer;
                $modelsAddress = $this->getaddressess($modelCustomer->id);
    
        //...................
       }
    
    
    
        public function getaddressess($id)
        {
          $model = Address::find()->where(['id' => $id])->all();
          return $model;
        }
    

    您还需要添加上面共享的内容

    在更新视图文件上:

    'model' => $model,
    'modelsAddress'=>$modelsAddress,
    
    希望这有帮助。在Po.php模型中,它对我很有用:

    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
        ]);
    }
    

    我也面临着同样的问题,如果您能回答,我们将不胜感激……您有哪些错误?更具体一点。请提供操作创建代码…获取未知属性:backend\models\Importpending::address请参见上文我在对$address进行验证时更新了我的问题,当am updateno working不适用于我们时,也呈现空字段。。。到底是什么不起作用?你有什么错误?
    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
        ]);
    }