Possibly A Bug? $Model->Save() Not Inserting A New Record

Hello,

Please let me know after reading the following if you feel this is a bug or not:

I spent quite a lot of time, and was very puzzled about the following behaviour:

$model = new Identiteitsdocument(); // English: "Identity proving document (passport, ID Card etc)

$isNew = $model->isNewRecord; // returned false!! but it should return true!!

if(isset($_POST[‘Identiteitsdocument’]))

{

$model->attributes=$_POST[‘Identiteitsdocument’];

        $isSaved = $model->save(); //returned true, but when checking the database, nothing was inserted!


    if($isSaved)  {


            $persoon = Persoon::model()->findByPk($persoon_id);


            if (!is_null($persoon)) {


                $persoon->identiteitsdocument_id = $model->id; // model->id was NULL 


                $persoon->save();


            }               


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


        }

}

The attributes were correctly assigned by massive assignment from the posted form input fields,

but even though $model->save() evaluated to TRUE, the record was not saved in the database.

The bug-like behaviour starts with the fact that $model->isNewRecord evaluated to FALSE, immediately after creating a new instance of class Identiteitsdocument.

And it continues with evaluating $model->save() to TRUE even though there is no record inserted in the database table.

I finally found what was causing this strange behaviour: I had used an empty constructor in the class file like this:

function __construct()

{

// $this->documenttype = null;


// $this->number = null;


// $this->validuntil_date = null;

}

I had done this because I was not sure what the values of the attributes would be after calling new Identiteitsdocument() if I did not use a constructor. I found that the assignments to null were causing problems, so I commented these three statements out.

When I removed the whole function __construct() from my code, immediately everything worked like normal.

I feel that using an empty function __construct() should not cause otherwise correct code to start behaving like I described above. The time saved by using the Yii Framework (about which I am still quite positive) will be used up by trying to figure out in the debugger what is actually happening with otherwise correct code.

Looking forward to hearing your views on this.

Greetings from a fairly new Yii Framework user, Martin de Groot

Your empty constructor did not call the parent constructor. That’s it. Refer to the source of CActiveRecord.

I totally agree with nineinchnick!