我试图在更新操作中保存另一个表中的字段。这是我的更新操作。
public function actionUpdateResumo($id)
{
$model = $this->findModel($id);
$model->situacao = $model->nginAgentDetail->situacao;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$nginAgentDet = NginAgentDetail::find()
->where(['CLIENT_ID'=>$id])->one();
$nginAgentDet->situacao = Yii::$app->request->post()['EtuLoja']['situacao'];
if ($model->save() && $nginAgentDet->save()) {
return $this->redirect(['view', 'id' => $model->ID]);
} else {
var_dump($nginAgentDet->getErrors());die;
}
} else {
return $this->render('update-resumo', [
'model' => $model,
]);
}
}
当我点击更新时,它会显示消息:
客户ID无效。
客户端ID是另一个表中的字段(NginAgentDetail)
在我的模型中,我有这样的关系:
public function getNginAgentDetail()
{
return $this->hasOne(NginAgentDetail::className(),['CLIENT_ID' => 'ID']);
}
在另一张表中我有这种关系:
public function getEtuLojas()
{
return $this->hasMany(EtuLoja::className(), ['ID' => 'CLIENT_ID']);
}
NginAgentDetail模型:
class NginAgentDetail extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'ngin_agent_detail';
}
public function rules()
{
return [
[['situacao'], 'string'],
// [['valMoeda'], 'number'],
[['CLIENT_ID'], 'integer'],
[['OPERATION_DATE', 'CATEGORY', 'MOV_TYPE', 'VALUE', 'COMMISSION', 'AFTER_BALANCE', 'ORIGIN_DESTINY', 'VALUES', 'USER'], 'string', 'max' => 100],
[['CLIENT_ID'], 'exist', 'skipOnError' => true, 'targetClass' => MasterLoja::className(), 'targetAttribute' => ['CLIENT_ID' => 'id']],
];
}
public function attributeLabels()
{
return [
'id_pk' => 'Id Pk',
'OPERATION_DATE' => 'Operation Date',
'CLIENT_ID' => 'Client ID',
'CATEGORY' => 'Category',
'MOV_TYPE' => 'Mov Type',
'VALUE' => 'Value',
'COMMISSION' => 'Commission',
'AFTER_BALANCE' => 'After Balance',
'ORIGIN_DESTINY' => 'Origin Destiny',
'VALUES' => 'Values',
'USER' => 'User',
'situacao' => 'Status',
];
}
public function getEtuLojas()
{
return $this->hasMany(EtuLoja::className(), ['ID' => 'CLIENT_ID']);
}
public function getMasterLoja()
{
return $this->hasMany(MasterLoja::className(), ['id' => 'ORIGIN_DESTINY']);
}
}
FindModel函数:
protected function findModel($id)
{
if (($model = EtuLoja::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
修改
我发现了导致客户ID无效消息的原因。这是 NginAgentDetail 模型中的验证规则:
[['CLIENT_ID'], 'exist', 'skipOnError' => true, 'targetClass' => MasterLoja::className(), 'targetAttribute' => ['CLIENT_ID' => 'id']],
现在的问题是它似乎是保存到数据库,但是当我返回到gridview时,属性situacao保持不变,我的意思是它没有更新。