[DDC-1847] Do not check for type equality in scalars when computing changeset? Created: 30/May/12  Updated: 08/Jun/12

Status: Open
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor
Reporter: Albert Casademont Assignee: Benjamin Eberlei
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Decimal type in mysql converts as a string in php. This is great as Decimal has a much higher precision than a float or double and that precision would be lost if converted to a float in PHP. Fine! But when doing calculations (as my numbers do not require an enormous precision gmp_ functions are not necessary) php converts these strings into floats. Then, when computing the changeset, as the value is compared with === is marked as a change even though there is none ("5.00" string vs 5.00 float) and an UPDATE for that row is made. Would it be possible to check only for simple equality "==" instead of type equality "===" when dealing with scalar types?

Another example of this would be the boolean type, that it is stored as an integer 1 in mysql but converted to a boolean true in php. If during the execution of my code that boolean gets converted to an integer 1, that will trigger an UPDATE also because 1 !== true.

Should this be my responsability or doctrine should be a little more flexible regarding comparisons? Thanks!!



 Comments   
Comment by Marco Pivetta [ 08/Jun/12 ]

Hi there!
Actually, doctrine orm converts floats from DB string to double at https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Types/FloatType.php#L52 . Keeping the correct type in for your fields is up to you, so be sure to cast in every setter

Comment by Albert Casademont [ 08/Jun/12 ]

Hi marco!

Actually i am using DECIMAL (or NUMERIC), not FLOAT, That type is not casted as it would lose precision. Therefore, my problem is when working with DECIMAL (Which is, btw, the type that mysql recommends for storing money values)
thet
https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Types/DecimalType.php

Comment by Marco Pivetta [ 08/Jun/12 ]

Unsure if the cast should happen in the type (just ignorant about the implication in precision), but I'll suggest it then.

Comment by Albert Casademont [ 08/Jun/12 ]

It should not happen as the DECIMAL type in MySQL has much more precision than a double or float in PHP. It was previously cast but there was an issue regarding this cast and the cast was deleted

http://www.doctrine-project.org/jira/browse/DBAL-121

After that, in another issue a user points out the same problem i am facing, that i have to cast back to string if i do not want doctrine to issue an UPDATE command for values that have not changed

http://www.doctrine-project.org/jira/browse/DBAL-180

As i said, my only point is that maybe, when computing the changeset, the comparison for scalar types should be more relaxed with a == instead of a ===

Comment by Marco Pivetta [ 08/Jun/12 ]

Don't think this can be done, as you don't really know what types (and so also the conversion rules) the user applies to his own model. I wouldn't do that, leaving the implementor of the entities to have strict checks on types during operations in setters...

Generated at Fri Oct 31 08:35:12 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.