Yii Framework Forum: Save() doesnt set id - Yii Framework Forum

Jump to content

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

Save() doesnt set id Rate Topic: -----

#1 User is offline   bigbasher 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 23-February 11

Posted 29 March 2011 - 03:57 PM

I am saving a new record using the standard yii mechanism but I have the id column which is the PK as a control on the form so I can load a previous instance. The code that does stuff after the save() relies on $model->id. Prior to adding the dropdownlist that is linked to id this worked fine. Now I find that although save() does actually save the new entity, and in the log I can see an insert statement, the id property which is the PK is not set so I run into trouble further on.

Any ideas why the id would not be set when the insert has successfully happened and save returned true?
0

#2 User is offline   Command 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 06-September 10
  • Location:Berlin

Posted 29 March 2011 - 04:09 PM

is the id field set to auto increment?
are the ids set in the table?
0

#3 User is offline   bigbasher 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 23-February 11

Posted 29 March 2011 - 04:17 PM

Yes and Yes. The same model works fine in another controller and used to work in this one until I added the code to reload older entities - this seems to have thrown it. The changes are to add a drop down list with all entities and then when this is selected to load an older one into the form.

On the form I added
    	<label for="transTo">Choose recipient:</label>
        <?
          echo CHtml::activeDropDownList($model,'id',        CHtml::listData(Recipient::model()->findAllByAttributes(array('customer_id'=>yii::app()->user->getState('c_id',-1))), 'id', 'NameAndCountry'),
          array('empty'=>array(0=>'Resend to a previous recipient'),
                'class'=>'wideSelect',
                'submit'=>CController::createUrl('/sendAmount/index/'),
                ));
            ?>


The in the controller I load the model if no buttons were pressed and ID is passed in. Problem is that now when there is no ID (I have tried 0 or '' in the id property prior to save) save() works fine but when I trace immediately after save() I dont see a value in the id property.

if($model->save()) {
  yii::trace($model->id.':'.$model->Surname,'**************************************after save');


but there is a new row in the db and I see in the log that an insert happpened.
1

#4 User is offline   bigbasher 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 23-February 11

Posted 30 March 2011 - 02:59 AM

View Postbigbasher, on 29 March 2011 - 04:17 PM, said:

Yes and Yes. The same model works fine in another controller and used to work in this one until I added the code to reload older entities - this seems to have thrown it. The changes are to add a drop down list with all entities and then when this is selected to load an older one into the form.

On the form I added
    	<label for="transTo">Choose recipient:</label>
        <?
          echo CHtml::activeDropDownList($model,'id',        CHtml::listData(Recipient::model()->findAllByAttributes(array('customer_id'=>yii::app()->user->getState('c_id',-1))), 'id', 'NameAndCountry'),
          array('empty'=>array(0=>'Resend to a previous recipient'),
                'class'=>'wideSelect',
                'submit'=>CController::createUrl('/sendAmount/index/'),
                ));
            ?>


The in the controller I load the model if no buttons were pressed and ID is passed in. Problem is that now when there is no ID (I have tried 0 or '' in the id property prior to save) save() works fine but when I trace immediately after save() I dont see a value in the id property.

if($model->save()) {
  yii::trace($model->id.':'.$model->Surname,'**************************************after save');


but there is a new row in the db and I see in the log that an insert happpened.


I have now solved this - I am not sure it isnt a bug. I have to set the PK to null before save.

            if ($model->id==0) {
                $model->id=null;
            }
            if($model->validate())
            {
                // form inputs are valid, do something here
                if($model->save()) {


0

#5 User is offline   Renaud 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 04-October 11

Posted 05 October 2011 - 09:06 AM

Indeed I've witnessed the same problem. I think it could be because the id in the form is set to empty string (has to be null like suggested). The id is set in the database properly.
1

#6 User is offline   Revelis Luc Bonnin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 23-May 11
  • Location:Earth

Posted 16 February 2012 - 03:57 AM

I had the same issue and forcing $model->id = null; before save fixed it.

In my context, i used the same form for create and update so my posted data returned id=0 when i'm creating a new row.
I affect $model->attributes = $_POST['mydata'] and $model->id = 0 <_<
0

#7 User is offline   Sarfraz Sarwar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 19-December 10

Posted 10 June 2013 - 11:41 AM

did not work for me.

$model->id=0;

resulted in error

and

$model->id=null;

had no effect
0

#8 User is offline   Sarfraz Sarwar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 19-December 10

Posted 10 June 2013 - 11:44 AM

this didn't work either
$model->getPrimaryKey();
0

#9 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 654
  • Joined: 23-October 10
  • Location:Romania

Posted 11 June 2013 - 12:38 PM

That's a pretty old bug, here you can find the issue and a dirty fix: https://github.com/y...yii/issues/2528
0

#10 User is offline   duri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 18-December 14

Posted 20 January 2015 - 09:32 AM

I had the same issue. To resolve your problem, do not bind your primary key attribute in your partial view. For example when I add this line to my partial view:

<?php echo $form->hiddenField($model,'id'); ?>


then after
$model->save()
, my primary key is empty.
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