Problem with float on MySQL

Hi,

I have problems getting float fields from MySQL.

All attributes float, int, tinyint of my activerecord models appears to be string when coming from the db.

i.e the float (double) field wit value 23 is printed as 000000000023 by yii details widtet.

I have read some posts about the int size difference between php and mysql but that applyes to double too?

Is strange too that when using mssqlserver all the float fields are ok and printed as double.

This could be a mysql configuration issue?

Thnaks in advance for your answers.

Cheers

Sebastian

HI Sebastian

can you show me you table schema

can you please the contents of rules method of your AR?

CREATE TABLE IF NOT EXISTS typetest (

id int(11) NOT NULL AUTO_INCREMENT,

floatCol float NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=2 ;

INSERT INTO typetest (id, floatCol) VALUES

(1, 23);

I’m actually using giix to create my models and cruds but when you and seenivasan asked about this basic things my first reaction was to test again using only standard gii.

Using gii the float value 23 is printed as 23 but the value is still the "23" string in the attributes collection of my model. The integer id is a String too but taken account of other posts in this forum that is correct.

I should repeat the same test using gii over mssql server.

Cheers

Sebastian

Hi,

the gii generated rules are:




	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('floatCol', 'required'),

			array('floatCol', 'numerical'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, floatCol', 'safe', 'on'=>'search'),

		);

	}

Hi

I looking deeper in my mssql server tests and al float values are strings too in the attributes collection as one can see from this debugger screenshot. I think that I’m becoming a little nervous with this issue but is too strange for me how this is managed by ar.

3108

debug.PNG

I haven’t tested this to see if it’s the problem in your case, but you generally assign the float like


'floatCol' float(n,d) NOT NULL

Where ‘n’ is the total number of digits and ‘d’ is the number of digits allowed after the decimal point.

For example float(6,2) would allow a value like 9999.99

Give it a try and see if it helps.

change your float columns into following format


'columnName' float(n,d)

I have the same problem but with ‘int’ columns. The AR properties are all strings even though it is specified as ‘int’ in the database.

Even though the value of the primary key column is represented as string in PHP.

Also, the int column allows NULL values and the NULL values become 0 in PHP and that is a big difference!

Any help would be appreciated!

Hi Atis:

You should learn to use Behaviors (i.e. http://www.yiiframework.com/extension/i18n-datetime-behavior). I think is an intermediate solution.

In other post I have read about the posibility of using the MySQL native driver to get the PHP native types and not plain strings but I have not tested this yet.

So if you make any progress on this I will be interested.

Cheers

Sebastian