The new record is not saved and there are no error messages

I am trying to save a new line to a quote, but Yii does not save QuoteRow. It saves only the new service (yes, the database structure is a bit strange). I don't seem to understand. If the string is not saved, $ qr-> save () returns false, but it is not. The service was successfully inserted, but there is no cover.

$service = new Services; $service->label = $row['title'] ?: "Övrigt"; $service->is_priced_per_unit = 1; $service->price_per_unit = $row['price']*0.8; $service->is_default = 0; $service->rot_deductable = (int)isset($row['rot']); $service->rot_deduction_percentage = 0.5; if (!$service->save()) $this->addError('Kunde inte spara raden',$service->getErrors()); else{ $qr = new QuoteRows; $qr->quote_service_id = Yii::app()->db->getLastInsertID(); $qr->quote_id = $id; $qr->unit_size = $row['amount'] ?: 0; $qr->raw_price = $row['price']*0.8*($row['amount'] ?: 1); $qr->is_rot_deductable = isset($row['rot']) ? 1 : 0; $qr->is_active = 1; if (!$qr->save()) $this->addError('Kunde inte spara raden',$qr->getErrors()); } 

If $ qr is not saved, I should get errors. I also tried checking $ qr with the validate function, and it claims to be perfectly true!

+1
source share
5 answers

Use debug mode or use the beforeValidate, beforeSave method with print_r (model $) in the model;

If all attributes are set, new data must be saved; something obvious is missing.

+1
source

I had the same problem, but I forgot the beforSave method. It returned false, but I did not set any errors. Therefore, the returned array of the getErrors method was empty.

+1
source

Make sure the beforeSave, afterSave functions return true

+1
source

You are using two models in one controller. It is best to import all the fields in QuoteRows into Services publicly into Services Model. And storing the values ​​using the POST method, and save it using the QuoteRows model. You can also add conditions for verification in the "Service Model".

0
source

You are probably using an event in your model that does not return true. eg. public function beforeSave() {....... return true;//must return true after everything}

0
source

Source: https://habr.com/ru/post/1236173/


All Articles