Anton Gildebrand
Anton Gildebrand

Reputation: 3707

New record isn't saved and no error messages

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!

Upvotes: 1

Views: 5082

Answers (5)

Malki Mohamed
Malki Mohamed

Reputation: 1688

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}

Upvotes: 0

Jijo
Jijo

Reputation: 1

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.

Upvotes: 0

Stanislav
Stanislav

Reputation: 913

Make sure your beforeSave, afterSave functions return true

Upvotes: 1

Amir Hossein Jamsidi
Amir Hossein Jamsidi

Reputation: 2060

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.

Upvotes: 1

Ionut Flavius Pogacian
Ionut Flavius Pogacian

Reputation: 4801

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.

Upvotes: 1

Related Questions