Weird ActiveRecord Record Insert Issue

Hello everyone,

I am experiencing a strange issue with ActiveRecord. I am attempting to create a new record for a user, and each time it fails, although all of the POST data validates without any errors and Yii displays no errors, either.

I’ve ensured that all fields that are not allowed to be null by MySQL have data, and in fact, I can insert new records into the table in question via phpMyAdmin with no problem. I even created a method that prints out the SQL generated by ActiveRecord prior to the insert operation, and that SQL executes in phpMyAdmin without error (after I replace the bound parameters with legitimate values).

I’m sure I am missing something simple and obvious, but for the life of me, I cannot seem to find it.

Below is the code from my UserController’s method that is supposed to create the new record:




class UserController extends Controller

{

   Other stuff ...


    public function actionRegisterStudent()

    {

        $student = new User; 

        $student->setScenario('registerStudent'); 

        

        // $this->performAjaxValidation($student, 'school-inquiry-form');


        if (isset($_POST['User']))

        {

            $student->attributes = $_POST['User']; 

            $student->created = new CDbExpression('NOW()'); 

            

            $attr = print_r($student->getAttributes(), TRUE); 

            Yii::trace("Student attributes after POST: {$attr}"); 

            

            if($student->save()) 

            {

                Yii::app()->user->setFlash('success', "Congratulations! Your account has been created.");

                // $this->redirect(Yii::app()->user->returnUrl);

            }

            else

            {

                Yii::trace("Could not save the user. SQL: {$student->getInsertCommand()}");

            }

        }


        $this->render('studentRegForm', array('student'=>$student));

    }

}



Any assistance would be much appreciated. TIA!

beforeSave() method in User model not calling parent/returning true? Behaviors attached?

/Tommy

Tommy,

Thank you for pointing these out. I had not thought of them. However, I am calling parent::beforeSave() in the beforeSave() method. Should I move it from the if() statement to the end (or beginning) of the method, maybe?

I do have a behavior attached, but it is the one I created to print out the generated SQL, and the model was already not inserting new records before I attached the behavior.

Also, I don’t know that this would affect record creation operations, but I should mention that I do have the srbac extension installed.

Here is the code for the beforeSave() event and the behavior I created:




    public function beforeSave()

    {

        if (parent::beforeSave())

        {

            if ($this->isNewRecord)

            {

                $this->created = new CDbExpression('NOW()');

                $this->status = self::STATUS_PENDING;

            }

        }

    }


    public function behaviors()

    {

        return array(

            'DebugSqlBehavior'=>array('class'=>'application.components.behaviors.CDebugSqlBehavior'),

        );

    }



What about "return true;" in the end of beforeSave() ? This method must always return a boolean value.

That was the problem. Thanks!

It seems like you’d want to make your changes before calling the super class’s implementation, just in case your changes break something:




public function beforeSave(){


    // Make changes

    if ($this->isNewRecord){

        $this->created = new CDbExpression('NOW()');

        $this->status = self::STATUS_PENDING;

    }


    // Hand off to parent

    return parent::beforeSave();

}



Just a thought…