New record isn't saved and no error messages

2019-06-08 06:15发布

I am trying to save a new row into a quote, but Yii doens't save the QuoteRow. It only saves the new service (Yes the DB-structure is a bit weird). I can't seem to figure it out. If the row doesn't get saved, $qr->save() should return false but it doesn't. The service is successfuly inserted, however the quoterow isn't.

$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 isn't saved, i should get the errors. I've also tried to validate $qr by using the validate-function, and it claims that it is perfectly valid!

5条回答
ゆ 、 Hurt°
2楼-- · 2019-06-08 06:47

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

查看更多
欢心
3楼-- · 2019-06-08 06:50

You are using two models in the same controller. It is better to import all the fields in 'QuoteRows' to 'Services' publicly in 'Services model'. And while saving get the values using the POST method and save it through QuoteRows model.You can add conditions for validation in 'Service Model' too.

查看更多
手持菜刀,她持情操
4楼-- · 2019-06-08 07:07

Either use debug mode, or use a beforeValidate, beforeSave method with print_r($model)within the model;

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

查看更多
神经病院院长
5楼-- · 2019-06-08 07:07

Make sure your beforeSave, afterSave functions return true

查看更多
一夜七次
6楼-- · 2019-06-08 07:09

I had the same problem but I had forgotten the beforSave method. It returned false but I had not set any errors. So the return array of getErrors method was empty.

查看更多
登录 后发表回答