StackExchange的长期被动用户,我在这里不知所措。它可能非常简单,但我被卡住了。我使用gii-enhanced来构建模型和CRUD。
我有两个模型:ordenes和facturas。
当我更新facturas模型时,我可以将它链接到已经存在 现有的ordenes模型。
facturas模型可以链接到ordenes模型。
Ordenes有一个金额字段,facturas也是如此。
我目前所拥有的:我可以将facturas模型链接到已经存在的ordenes模型。一个facturas模型只能有一个ordenes模型,而ordenes模型可以有多个与之相关的facturas模型。
我想要实现的是,当我将 facturas 模型链接到ordenes模型时,从 ordenes 的数量中减去facturas模型的数量model和保存到 ordenes 表的新值。因此,如果我已经有一个 ordenes 模型,其金额为200000美元,并且链接到一个金额为$ 150000的 facturas 模型,则更新的 ordenes 模型的金额应为$ 50000。
facturasController中的相关代码:
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
],
]); ?>
facturas模型的相关部分:
/**
* @return \yii\db\ActiveQuery
*/
public function getOrdenesIdordenes()
{
return $this->hasOne(\frontend\models\Ordenes::className(), ['idordenes' => 'ordenes_idordenes'])->inverseOf('facturas');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProveedoresIdproveedores()
{
return $this->hasOne(\frontend\models\Proveedores::className(), ['idproveedores' => 'proveedores_idproveedores'])->inverseOf('facturas');
}
/**
* @return \yii\db\ActiveQuery
*/
/*public function getUserIduser()
{
return $this->hasOne(\frontend\models\User::className(), ['id' => 'user_iduser'])->inverseOf('facturas');
}*/
public function getUserIduser()
{
return $this->hasOne(\backend\models\User::className(), ['id' => 'user_iduser'])->inverseOf('facturas');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getOrdenes()
{
return $this->hasMany(\frontend\models\Ordenes::className(), ['facturas_idfacturas' => 'idfacturas'])->inverseOf('facturasIdfacturas');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getEstadosContab()
{
return $this->hasOne(\frontend\models\EstadosContab::className(), ['idcontab' => 'estados_contab'])->inverseOf('facturas');
}
/**
* @inheritdoc
* @return array mixed
*/
public function behaviors()
{
return [
'blameable' => [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'user_crea',
'updatedByAttribute' => 'user_modif',
],
'uuid' => [
'class' => UUIDBehavior::className(),
'column' => 'idfacturas',
],
];
}
/**
* @inheritdoc
* @return \frontend\models\FacturasQuery the active query used by this AR class.
*/
public static function find()
{
return new \frontend\models\FacturasQuery(get_called_class());
}
}
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());
}
}
答案 0 :(得分:2)
这只是一个建议 你应该填充模型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,
]);
}
}
根据您的上一条评论,这可能是您问题的更准确答案
假设订单模型是名称订单 在Facturas中,您有一个名为fk_orders的字段,您可以在该字段中传递用户在视图中选择的值,并且可以链接相关的订单
然后在Facturas Controller中你可以
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,
]);
}
}