Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1847

Do not check for type equality in scalars when computing changeset?

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: ORM
    • 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!!

        Activity

        Hide
        Marco Pivetta added a comment -

        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

        Show
        Marco Pivetta added a comment - 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
        Hide
        Albert Casademont added a comment -

        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

        Show
        Albert Casademont added a comment - 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
        Hide
        Marco Pivetta added a comment -

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

        Show
        Marco Pivetta added a comment - Unsure if the cast should happen in the type (just ignorant about the implication in precision), but I'll suggest it then.
        Hide
        Albert Casademont added a comment -

        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 ===

        Show
        Albert Casademont added a comment - 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 ===
        Hide
        Marco Pivetta added a comment -

        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...

        Show
        Marco Pivetta added a comment - 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...

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Albert Casademont
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: