Model Yii2-从更新表单更新另一个模型的单个字段

Model Yii2-从更新表单更新另一个模型的单个字段,model,controller,yii2,yii2-advanced-app,yii2-model,Model,Controller,Yii2,Yii2 Advanced App,Yii2 Model,StackExchange的长期被动用户,我在这里不知所措。这可能非常简单,但我被卡住了。我已经使用gii增强来构建模型和CRUD 我有两个模型:ordenes和facturas 当我更新facturas模型时,我可以将它链接到一个已经存在的 现有的ordenes模型 facturas模型可以链接到ordenes模型 Ordenes和facturas都有数量字段 我目前拥有的:我可以将facturas模型链接到现有的ordenes模型。一个facturas模型只能有一个ordenes模型,而

StackExchange的长期被动用户,我在这里不知所措。这可能非常简单,但我被卡住了。我已经使用gii增强来构建模型和CRUD

  • 我有两个模型:ordenes和facturas

  • 当我更新facturas模型时,我可以将它链接到一个已经存在的 现有的ordenes模型

  • facturas模型可以链接到ordenes模型

  • Ordenes和facturas都有数量字段

我目前拥有的:我可以将facturas模型链接到现有的ordenes模型。一个facturas模型只能有一个ordenes模型,而一个ordenes模型可以有多个facturas模型链接到它

我想实现的是,当我将facturas模型链接到ordenes模型时,facturas模型中的数量将从ordenes模型的数量中减去,并将新值保存到ordenes表中。因此,如果我已经有了一个ordenes模型,金额为200000美元,并链接到一个facturas模型,金额为150000美元,那么更新后的ordenes模型应该有50000美元

制造商控制器中的相关代码:

public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->loadAll(Yii::$app->request->post()) && $model->saveAll()) {
            return $this->redirect(['view', 'id' => $model->idfacturas]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
视图中的相关字段:

<?= $form->field($model, 'ordenes_idordenes')->widget(\kartik\widgets\Select2::classname(), [
        'data' => \yii\helpers\ArrayHelper::map(\frontend\models\Ordenes::find()->orderBy('idordenes')->asArray()->all(), 'idordenes', 'numero'),
        'options' => ['placeholder' => Yii::t('app', 'Vincular Orden de Compra')],
        'pluginOptions' => [
            'allowClear' => true
        ],
    ]); ?>
以及Ordenes模型的相关部分:

 /**
     * @return \yii\db\ActiveQuery
     */
    public function getFacturas()
    {
        return $this->hasMany(\frontend\models\Facturas::className(), ['ordenes_idordenes' => 'idordenes'])->inverseOf('ordenesIdordenes');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getNotas()
    {
        return $this->hasMany(\frontend\models\Notas::className(), ['ordenes_idordenes' => 'idordenes'])->inverseOf('ordenesIdordenes');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEstado()
    {
        return $this->hasOne(\frontend\models\Estados::className(), ['idestados' => 'estado_id'])->inverseOf('ordenes');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getNotasIdnotas()
    {
        return $this->hasOne(\frontend\models\Notas::className(), ['idnotas' => 'notas_idnotas'])->inverseOf('ordenes');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFacturasIdfacturas()
    {
        return $this->hasOne(\frontend\models\Facturas::className(), ['idfacturas' => 'facturas_idfacturas'])->inverseOf('ordenes');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProveedoresIdproveedores()
    {
        return $this->hasOne(\frontend\models\Proveedores::className(), ['idproveedores' => 'proveedores_idproveedores'])->inverseOf('ordenes');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTipocompra()
    {
        return $this->hasOne(\frontend\models\TipoCompra::className(), ['category_id' => 'tipocompra_id'])->inverseOf('ordenes');
    }

/**
     * @inheritdoc
     * @return array mixed
     */ 
    public function behaviors()
    {
        return [
            'blameable' => [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',
            ],
            'uuid' => [
                'class' => UUIDBehavior::className(),
                'column' => 'idordenes',
            ],
        ];
    }

    /**
     * @inheritdoc
     * @return \frontend\models\OrdenesQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \frontend\models\OrdenesQuery(get_called_class());
    }
}

这只是一个建议
您应该填充模型Facturas,检索与此订单相关的Facturas,然后将您需要的值正确分配给Facturas模型

use common\models\Facturas;

public function actionUpdate($id)
    {
        $model = $this->findModel($id);


        if ($model->loadAll(Yii::$app->request->post()){
           $modelFacturas = Facturas::find()
             ->(['your_id_fatcuras' => $model->your_id_facturas ])->one();
           $modelFacturas->you_field_to_change =  $model->value1 - $modelFacturas->value2;
           $model->save();
           $modelFacturas->save();

           return $this->redirect(['view', 'id' => $model->idfacturas]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
根据你最后的评论,这可能是对你问题的更准确的回答

假设您的订单模型是命名订单 事实上,您有一个名为fk_orders的字段,您可以在其中传递用户在视图中选择的值,并可以链接相关的顺序

那么事实上你可以

use common\models\Orders;

public function actionUpdate($id)
    {
        $model = $this->findModel($id); // retrive the Factursa model


        if ($model->loadAll(Yii::$app->request->post()){
           $modelOrders = Orders::find()
             ->(['id_orders' => $model->fk_orders ])->one();
           $modelOrders->amount  =  $modelOrders->amount - $model->value_to_subtract;
           $model->save();
           $modelOrders->save();

           return $this->redirect(['view', 'id' => $model->idfacturas]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

问题是,Facturas模型最初创建时没有相关的订单模型。当用户更新Facturas模型时,将创建一个关系(通过选择框)。在这一点上,Facturas模型的monto(金额)应该从新关联订单模型的monto中减去。所有这些都来自Facturas模型的更新视图。然后主模型是Fatcuras模型,提交-返回也是订单模型的参考?实际上,设置如下:首先,创建Ordenes模型。随后还创建了Facturas模型。两者都有一个金额字段,详细说明该文档的价值。在某一点上,Facturas模型必须与Ordenes模型相关联。一个Ordenes模型可以有多个Facturas模型关系,而Facturas模型只能有一个Ordenes关系。其思想是,当Facturas模型通过选择框链接到Ordenes模型时,Facturas和Ordenes模型都必须更新,但在不同的属性(Facturas的Ordens FK,Ordenes的数量)上,逻辑是在post和save之间,控制器将使用FK查找Ordenes模型,然后进行数学运算,最后保存两个模型,然后返回视图id。我现在明白了。很好。但是,我在loadAll行遇到了一个
语法错误,意外的“{”
。我注意到,
if($model
的第一个括号没有闭合。这可能是原因吗?
use common\models\Orders;

public function actionUpdate($id)
    {
        $model = $this->findModel($id); // retrive the Factursa model


        if ($model->loadAll(Yii::$app->request->post()){
           $modelOrders = Orders::find()
             ->(['id_orders' => $model->fk_orders ])->one();
           $modelOrders->amount  =  $modelOrders->amount - $model->value_to_subtract;
           $model->save();
           $modelOrders->save();

           return $this->redirect(['view', 'id' => $model->idfacturas]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }