Save not saving

Hi,

I am currently trying to save records using the save() method however it is not saving the record to the DB?

I have set the false variable as a parameter in the save method but it still does not work.

What reason would it not "save" even when I have told it not to validate?

James.

There is possibility you forgot to return in beforeSave() or you are inserting invalid value to the database…

Brilliant, you are a life saver. It was the beforeSave that was problem.

Hi, I also encounter a problem with save().

Here is my code:

ProfilesController.php




public function actionRegister()

	{

    $model=new RegisterForm;


    // uncomment the following code to enable ajax-based validation

    /*

    if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')

    {

        echo CActiveForm::validate($model);

        Yii::app()->end();

    }

    */


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

    {

        $model->attributes=$_POST['RegisterForm'];

		

        if($model->validate())

        {

            // form inputs are valid, do something here

			

			$profile=new Profiles;			

			$profile->id = "";

			$profile->u_id = $model->id;			

			$profile->lastname = "";

			$profile->firstname = "";

			$profile->date_of_birth = "";

			$profile->about = "";		

			

			if ($profile->validate()){

			$model->save();

			$profile->save(); //this does not save into db and return false

			}

			/* My hack method that works

			$sql = 'INSERT INTO {{profiles}} (id,u_id,lastname,firstname,date_of_birth,about) VALUES ("",:u_id,"","","","")';

			$parameters = array(":u_id"=>$model->id);

			Yii::app()->db->createCommand($sql)->execute($parameters);

			*/			

         

		  $this->redirect(Yii::app()->homeUrl);

           

        }

    }

    $this->render('register',array('model'=>$model));

	}



u_id is a foreign key which references to the table user.id

Can anyone please help me with this?

Check var_dump($profile->getErrors());

Also, when you call $model->validate() and then $model->save() the validation runs twice. Use $model->save(false) instead.

Thanks a lot andy_s, i fixed the error.