Yii Framework Forum: Possibly A Bug? $Model->Save() Not Inserting A New Record - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Possibly A Bug? $Model->Save() Not Inserting A New Record $model->isNewRecord is false directly after $model =new Rate Topic: -----

#1 User is offline   MartindeGroot 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-July 13

Posted 20 September 2013 - 08:22 AM

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
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 20 September 2013 - 10:34 AM

Your empty constructor did not call the parent constructor. That's it. Refer to the source of CActiveRecord.
Don't be a dick.
0

#3 User is offline   AlanCooper 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 02-October 13

Posted 02 October 2013 - 09:49 AM

I totally agree with nineinchnick!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users