Yii Framework Forum: Model Attribute Return 0 For Null Value - Yii Framework Forum

Jump to content

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

Model Attribute Return 0 For Null Value When an integer field in mysql is set to NULL, Yii returns 0 instead. Rate Topic: -----

#1 User is offline   monk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 22-March 11

Posted 28 September 2013 - 04:10 AM

I need help to differentiate between 0 and NULL values on Yii 1.1.8 models. Apparently, when an integer field in mysql is set to NULL, Yii returns 0 instead. I wonder if Yii converts the NULL value to 0 automatically? This should not be the case as 0 is not necessarily NULL. Please help.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,074
  • Joined: 16-February 11
  • Location:Japan

Posted 28 September 2013 - 08:36 AM

Hi monk,

PHP uses implicit type conversion frequently.
$value = NULL;
if ($value == 0) {
   echo 'Yes, it is zero. Ah, well, kind of ...';
}

Isn't it because you are comparing the value with the operator '==' ?
0

#3 User is offline   monk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 22-March 11

Posted 28 September 2013 - 08:51 AM

This has worked now. So sorry I was quick to raise it here. It was my fault I hadn't checked the data that was coming from mysql quite well.
0

#4 User is offline   monk 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 22-March 11

Posted 28 September 2013 - 08:53 AM

Yes, I was using '==' instead of checking for equality using '==='.
This works now. thanks.
0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,074
  • Joined: 16-February 11
  • Location:Japan

Posted 28 September 2013 - 09:06 AM

Don't be sorry, no problem.
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