Debugging CActiveRecord failed save() attempts

  1. What's the problem?
  2. Solution?

What's the problem?

When you're filling up a form of a CActiveRecord (using CActiveForm for example), usually you'll construct your code so that in case of validation error you'll get back the form with the error displayed back to you, typically when CActiveForm.errorSummary() is used. This is how its done by Gii.

Yet, on other cases like on related models handling or otherwise updates to the DB using CActiveRecord model objects where you do not present a form to your users, you'd sooner or later be presented with validation errors. At least in my case, even when working closely with the application.log file, I didn't see any reports in it (although I didn't put it into 'trace' mode as well - that's too much information).

Solution?

Simple: First, I recommend you start working with the logging system of Yii. You'll need to spit out some error messages and var_dump() is not cool or too professional, if you'd ask me. Wherever you're going to print those - you'll manually request error information from the model object and log it. Here's how it looks in my case:

Yii::log("errors saving SomeModel: " . var_export($someModelObject->getErrors(), true), CLogger::LEVEL_WARNING, __METHOD__);

Where $someModelObject is the model you've just tried to save(), and which is of type SomeModel. Examine you're target output and solve the problem(s).

4 0
2 followers
Viewed: 39 776 times
Version: 1.1
Category: Tips
Written by: Boaz
Last updated by: Boaz
Created on: May 21, 2013
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles