3

This is my user table structure :

+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| ID             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| screen_name    | varchar(20)      | NO   | UNI | NULL    |                |
| slug           | varchar(20)      | NO   |     | NULL    |                |
| email          | varchar(50)      | NO   | UNI | NULL    |                |
| pass           | varchar(32)      | YES  |     | NULL    |                |
| signin_twitter | enum('T','F')    | NO   |     | F       |                |
| twitter_id     | int(11)          | YES  | UNI | NULL    |                |
| bg_image       | varchar(50)      | YES  |     | NULL    |                |
+----------------+------------------+------+-----+---------+----------------+

I'm trying to add data to my database. (in my controller)

$model=new Users;
        //$this->performAjaxValidation($model);
        if(isset($_POST['Users']))
        {
                    //screen_name, email,pass comes from form. Also, we need to set **slug** and **singin_twitter**
                $_POST['Users']['slug'] = $this->sanitize($_POST['Users']['screen_name']);
                $_POST['Users']['signin_twitter'] = 'F';
            $model->attributes=$_POST['Users'];
            if($model->save())
                $this->redirect('dash/index');                  
        }

But it's not saving data . $model->save() returning FALSE . How can i debug it ?

tereško
  • 58,060
  • 25
  • 98
  • 150
Eray
  • 7,038
  • 16
  • 70
  • 120

10 Answers10

5

Yii debug tool bar is a very good debug tool yii-debug-toolbar download page

xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
4

For debugging activate the CWebLogRoute in the config/main.php. Uncomment:

                    array(
                            'class'=>'CWebLogRoute',
                    ),

if $model->save() return false, so saving data to the database could fail (for this: look ate the CWebLogOutput and/or MySQL Errors) or the validation could fail, because in general yii calls $model->validate before saving.

For testing, call $model->validate(); before $model->save() and check the return value. It is important, that all the attributes are "safe" before saving them to the database. If every attribute is valid/safe, so $model->validate(); returns true.

The Bndr
  • 13,204
  • 16
  • 68
  • 107
2

you should try $model->save(false); instead. If it requires validation, this will skip that and save your data successfully

Duc Tran
  • 6,016
  • 4
  • 34
  • 42
2
$_POST['Users']['slug'] = $this->sanitize($_POST['Users']['screen_name']);
$_POST['Users']['signin_twitter'] = 'F';
$model->attributes=$_POST['Users'];

You shouldn't use $model->attributes=$_POST['Users'] after a manual attribution, your field might be re-attributed if they are in the model rules.

Also to debug :

...
$model->attributes=$_POST['Users'];
$model->validate();
var_dump($model->getErrors());
if($model->save())
...

Note that you can use $model->save(false) to override the model validation.

Adri
  • 21
  • 1
2

$model->save(); returns false, when input validation fails. The first parameter of save() is boolean and says whether to run validation on attributes or not.

Read these two sections of yii definitive guide carefully:

You can get validation errors by calling getErrors():

$model->getErrors();
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
cebe
  • 3,610
  • 1
  • 23
  • 37
1

To know what is work, simple look at $model->getErrors();

Chux
  • 1,196
  • 1
  • 9
  • 24
0

To debug this kind of Yii active record problems (generally validation problems) i use:

$model->getErrors()

But when things get tricky, Yii::log() is very useful:

Yii::log($expressionOrVariable,'error');

It writes a line in the application log (inside the /protected/runtime folder), if you do not want to activate CWebLogRoute. Adding several log lines you can track what is going on and where.

Yii-debug-toolbar is a great extension also, but also very heavy if you work with javascript and firebug.

taseenb
  • 1,378
  • 1
  • 16
  • 31
0

Before you debug, if you are using MySQL check you db configuration has set to emulatePrepare=true. Add debug toolbar to trace all queries use this extension http://www.yiiframework.com/extension/yii-debug-toolbar

Suriyan Suresh
  • 2,964
  • 14
  • 51
  • 80
0

i'd like to use <?php echo $model->errorSummary();?> in the view.

pengemizt
  • 837
  • 1
  • 9
  • 16
0

First, your model should be called User not Users. The following will probably work as I tried this before. Any field that is not part of the table must be marked as "safe" in the model User else save will fail:

public function actionSaveUser()
{
    $model = new User;
    if(isset($_POST['User']))
    {
      $model->setAttributes( $_POST['User'] );
      if( $model->save() ) {
          // render success
      }
    }
    // render form
}

Let's say you need a field in the model User which is not part of the model's table. You need to mark it as safe else, for security reason, the save will fail if the "uglyName" is in the $_POST['User'].

class User extends CFormModel
{
  private $uglyName;

  /**
   * Declares the validation rules.
   */
  public function rules()
  {
    return array(
      array('uglyName', 'safe'),
    );
  }
}
vimdude
  • 4,447
  • 1
  • 25
  • 23