Answer the question
In order to leave comments, you need to log in
How to save data from a related table from a query to the database?
I have such a problem that there is an order and a client, I need to drive in the id of the client that I received in the order, but it does not work when I try to add the client received from the model, I want to save it in the order table in the id_client field.
This is what comes to me in the post request (debug from yii2)
$_POST Name
Value _csrf - frontend
'
dmZFSG95NmQkSzA9N0B1PCQ0BgouCGI8HjZ3GA5OWA4PUw0ZPA4HVg ==
' Ivan' 'phone' => '82147483647' 'email' => '' 'oplata' => '20' 'fact_oplata' => '10' 'srok' => '2017-07-30 18:20:00' 'status' => '' 'prioritet' => '' ] Client [ 'id' => '41' ]
public function actionUpdate($id)
{
$model = $this->findModel($id);
$client = new Client();
if ($model->load(Yii::$app->request->post())) {
$model->id_client = $client->load(Yii::$app->request->post());//Сюда должно вставиться из запроса 41, который получил от post-запроса
...
$model->validate();
if (!$model->save()) {
print_r($model->getErrors());
} else {
$model->save();
}
}
return $this->render('update', [
'model' => $model,
'client' => $client,
]);
}
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`crm`.`zakaz`, CONSTRAINT `zakaz_ibfk_6` FOREIGN KEY (`id_client`) REFERENCES `client` (`id `) ON UPDATE CASCADE)
The SQL being executed was: UPDATE `zakaz` SET `status`=0, `oplata`=20, `fact_oplata`=10, `number`=1, `phone`=2147483647, `id_client` =1 WHERE `id_zakaz`=1419
Answer the question
In order to leave comments, you need to log in
read carefully what the load method does, this code does not make sense
replace it with
$model->id_client = ArrayHelper::getValue(Yii::$app->request->post('Client'), 'id');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question