Yii Framework Forum: Attribute retrieved as string instead of int - Yii Framework Forum

Jump to content

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

Attribute retrieved as string instead of int Rate Topic: -----

#1 User is offline   Guyzeug 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-August 12

Posted 03 August 2012 - 03:46 AM

Hi,

I am quite new to Yii and PHP, and I've just seen a behaviour which I think isn't normal, that's why I've posted into the "Bug discussion category".
Also just so you know I've spent quite some time (hours) trying to debug this and searching on internet, but couldn't find anything (unless it's my english, as it's not my mother language...)

So in my Yii project, when I retrieve a CActiveRecord from the database, all the attributes of the Model I get are strings even if some columns are defined as int.

Example :
$user = Users::model()->findByPk(1);
var_dump($user->id)


This gives :
string(1) "1"


Is it normal that I get a "string" back from the database where the column was defined as "int" and the CActiveRecord "id" property has been generated by Gii as an Integer ?
I know PHP is dynamically typed but I would have expected Yii to cast the values retrieved from the database to the correct type, or am I missing something ?

I'm asking this because as I was assuming that the types were kept when retrieving models from the database, in my code I was comparing an id which I thought was a number to the number 2 using === and that was returning false...
 if ($user->id === 2) // returns false even if the user id is 2 in the database !


If the behaviour I'm seeing is correct, then what's the best approach when comparing model's attributes to some values, using "==" instead of "===", or still using "===" but forcing the type of the attribute I'm comparing ?

Thanks in advance for your help.

My configuration (sorry I can't put the versions I'm using as I'm posting from work) :
- Mac OS Lion
- Apache
- Safari
- MySQL
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,342
  • Joined: 12-October 09
  • Location:Croatia

Posted 03 August 2012 - 04:03 AM

This is by design and already discussed few times on the forum... problem is that database values can hold bigger numbers than PHP values so a string is used

For your comparison you can use
(int)$user_id === 2

Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Guyzeug 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-August 12

Posted 03 August 2012 - 05:12 AM

View Postmdomba, on 03 August 2012 - 04:03 AM, said:

This is by design and already discussed few times on the forum... problem is that database values can hold bigger numbers than PHP values so a string is used

For your comparison you can use
(int)$user_id === 2



Thanks for your quick reply, but I'm still a little bit confused.
I thought the issue you're talking about was more about when the Model is generated. For example I've read that if you define in MySQL a column as INT UNSIGNED then the corresponding Model attribute type will be "String" (generated by gii), which makes sense as the PHP Integer max value is less than the MySQL INT UNSIGNED max value.
However in my case I've defined a column as INT, and the corresponding Model attribute type has been set to "Integer" by Gii, and as far as I know the PHP Integer and MySQL INT types have the same range of values right ?

Also, if all the model attributes retrieved from the database are strings, why does gii then bother creating Integer attributes when generating the Model class if later on all the Models retrieved from the database have their attributes set to strings ?
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,342
  • Joined: 12-October 09
  • Location:Croatia

Posted 03 August 2012 - 05:36 AM

yes the "unsigned" part is true... what is the declaration of your ID filed in the database ?
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   Guyzeug 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-August 12

Posted 03 August 2012 - 06:16 AM

View Postmdomba, on 03 August 2012 - 05:36 AM, said:

yes the "unsigned" part is true... what is the declaration of your ID filed in the database ?


Here's my DB declaration :
CREATE TABLE IF NOT EXISTS tbl__user
(
    id int not null auto_increment,
    email_id int null,
    primary key (id)
)
ENGINE=InnoDB;


And the generated PHP code :
<?php

/**
 * This is the model class for table '{{user}}'.
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $id
 * @property integer $email_id
 *
 */
class User extends CActiveRecord {
  :
  :


But still when I retrieve a User from the database using User::model()->findByPk(...) it returns an object whose both "id" and "email_id" attributes are set with the "string" type.
0

#6 User is offline   lium 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 29-May 12

Posted 09 August 2012 - 01:39 PM

I'm interested in this too. I wonder if Yii automatically handles this?
0

#7 User is offline   Guyzeug 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 03-August 12

Posted 17 August 2012 - 04:19 AM

Hasn't anybody seen that problem before ? Or have I missed something ?
0

#8 User is offline   Sebastian Calvo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 14-July 12

Posted 25 August 2012 - 09:06 AM

Hi,

I'm interested on this too. For me is a weird behavior. There are a lot of posts in the forum regarding this problem and always the same answers :)

I think yii is a great, nice and well organized framework. You will found other problems on other frameworks so this is one I can live with it (for now).

Maybe using behaviours like http://www.yiiframew...tetime-behavior could help.

I think that if you don't have performance complains you can implement behaviors or other sort of magic to cast all fields to the right type.

Cheers
Sebastian
0

#9 User is offline   pavlepredic 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 30-August 11

Posted 13 December 2012 - 04:56 AM

How about this for a solution. Extend CActiveRecord and add the following methods:

public function populateRecord($attributes,$callAfterFind=true)
{
	if ($this->useTypeCasting() and is_array($attributes))
		foreach ($attributes as $name => &$value)
			if ($this->hasAttribute($name))
				settype($value, $this->getMetaData()->columns[$name]->type);

	return parent::populateRecord($attributes, $callAfterFind);
}

public function useTypeCasting()
{
	return false;
}


Then use this class as the base class for all your DB models. Override useTypeCasting() to return true in models where you want type casting.
This seems to work fine and will cast TINYINT and SMALLINT to PHP's integer (presumably MEDIUMINT too, haven't tested that). However, I'd really like to hear your thoughts on this, since I may be missing something. Is this safe to do and are there any significant performance hits from doing it? Does it cover all scenarios, or might there still be a case when a value will not be typecast? Obviously, when creating a model instance, you may still pass an incorrect type, but that's your own responsibility.
0

#10 User is offline   pavlepredic 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 30-August 11

Posted 13 December 2012 - 05:09 AM

Actually, you also need to check if value is NULL, so method should look like this:

public function populateRecord($attributes,$callAfterFind=true)
{
        if ($this->useTypeCasting() and is_array($attributes))
                foreach ($attributes as $name => &$value)
                        if ($this->hasAttribute($name) and $value !== null)
                                settype($value, $this->getMetaData()->columns[$name]->type);

        return parent::populateRecord($attributes, $callAfterFind);
}


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