H
H
hollanditkzn2017-07-24 19:14:48
Yii
hollanditkzn, 2017-07-24 19:14:48

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' ]

srok-zakaz-srok-disp '30 Jul 2017 18:20'
And in the controller I tried to specify
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,
        ]);
}

If you write like this, then it writes an error
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

And here I don’t understand how to insert the id received from the post request

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-07-25
@hollanditkzn

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 question

Ask a Question

731 491 924 answers to any question