[CLOSED] Integer primary key return as float after save() is called.

Dear guys,

I’m using Yii 1.1.7.

I have a problem when I create a new object for my model. And then I called save() to save data to DB. Finally, if save() works fine my model object will have value for its primary key.

For example.




$model = new Person();

$model->name = 'John';

$model->save();


echo $model->id; // This should be 1 or 2.. or whatever but it'll be integer as its table.



But what I got was 1.0, 2.0 or whatever value it is. But it’s a float type value. Basically, I created model + CRUD using Gii. I don’t know what is wrong. Everytime I need to use this data I need to call intval() and oh… no we can’t do that everytime…

If you have any idea, what could happen. Thanks a lot.

Closed

Got an answer. It’a bug.

Do you set INT in db scheme ?

It seems that you are not using a Int datatype for field id in your database table.

Please check it carefully.

Thanks for replying. That’s what I suspect but I didn’t.

Here is my MSSQL T-Script.




CREATE TABLE [dbo].[persons](

	[id] [int] IDENTITY(1,1) NOT NULL,

	[name] [varchar](50) NULL,

 CONSTRAINT [PK_persons] PRIMARY KEY CLUSTERED 

(

	[id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]


GO



So I can’t see what went wrong. This problem also happen to other models that I have. I can work now as I put intval() everywhere… but well… it isn’t normal.

known issue, look here for the fix:

http://code.google.com/p/yii/issues/detail?id=2312

But it very interesting that why this problem is appearing.Have you checked your model class also. Might be you have define some rule there or if you have define a beforeSave() or afterSave() then please also check it…

Thanks, that’s what I’m thinking. So I know how to fix it for now.

Thanks, for your apply. And I don’t have beforeSave() or afterSave() anywhere it’s just a normal CRUD. But as we can see it’s a bug in PDO. For now I just use intval().